.NET 文本转语音音量

发布于 2024-11-16 23:07:47 字数 219 浏览 1 评论 0原文

我正在使用 System.Speech.Synthesis 参考来处理一个简单的文本到语音应用程序。我想向应用程序添加一个滑块控件并用它控制语音的音量。为了设置我正在使用的音量:

speech.Volume = 100;

我​​是否需要使用某种事件处理程序来更新此值?顺便说一下,我使用 C#(请不要使用 VB.NET 代码)将其创建为 WPF 应用程序。

I am working with a simple Text to Speech application using the System.Speech.Synthesis reference. I would like to add a slider control to the application and control the volume of the speech with it. In order to set the volume I'm using:

speech.Volume = 100;

Do I need to use some kind of event handler in order to update this value? By the way I'm creating this as a WPF application with C# (please not VB.NET code).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往事风中埋 2024-11-23 23:07:47
<Slider Ticks="1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
            Value="1"
            Delay="100"
            Interval="5"
            TickPlacement="BottomRight"
            Minimum="1"
            Maximum="10"
            Width="100"
            AutoToolTipPlacement="BottomRight"
            ValueChanged="slider_ValueChanged"
            Grid.Row="1"
            Grid.Column="0">
    Slider>

创建 slider_ValueChanged 事件并设置 Speech.volume = (int)sliderID.value;

<Slider Ticks="1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
            Value="1"
            Delay="100"
            Interval="5"
            TickPlacement="BottomRight"
            Minimum="1"
            Maximum="10"
            Width="100"
            AutoToolTipPlacement="BottomRight"
            ValueChanged="slider_ValueChanged"
            Grid.Row="1"
            Grid.Column="0">
    Slider>

create event of slider_ValueChanged and set Speech.volume = (int)sliderID.value;

誰ツ都不明白 2024-11-23 23:07:47

添加两个滑块,sliderVolume 用于音量控制,sliderRate 用于速率控制。然后在 SpeakProgress 事件中,为 speech 分配新的音量和速率,并使用 characterPosition 创建原始阅读内容的子字符串。然后使用这个新的子字符串重新开始说话。请参阅以下代码。

    string selectedSpeakData = "Sample Text Sample Text Sample Text Sample Text Sample Text";
    private SpeechSynthesizer speech;

    private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                speech= new SpeechSynthesizer();
                speech.SpeakProgress += new EventHandler<System.Speech.Synthesis.SpeakProgressEventArgs>(speech_SpeakProgress);
                speech.SpeakAsync(selectedSpeakData);
            }

    void speech_SpeakProgress(object sender, System.Speech.Synthesis.SpeakProgressEventArgs e)
            {
                if (speech.Volume != Convert.ToInt32(sliderVolume.Value) || speech.Rate != Convert.ToInt32(sliderRate.Value))
                {
                    speech.Volume = Convert.ToInt32(sliderVolume.Value);
                    speech.Rate = Convert.ToInt32(sliderRate.Value);
                    selectedSpeakData = selectedSpeakData.Remove(0, e.CharacterPosition);
                    speech.SpeakAsyncCancelAll();
                    speech.SpeakAsync(selectedSpeakData);
                }
            }

Add two sliders, sliderVolume for Volume control and sliderRate for Rate control. Then in SpeakProgress event, assign new volume and rate to speech and by using characterPosition make a sub-string of original reading content. Then restart speak using this new sub-string. See the following code.

    string selectedSpeakData = "Sample Text Sample Text Sample Text Sample Text Sample Text";
    private SpeechSynthesizer speech;

    private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                speech= new SpeechSynthesizer();
                speech.SpeakProgress += new EventHandler<System.Speech.Synthesis.SpeakProgressEventArgs>(speech_SpeakProgress);
                speech.SpeakAsync(selectedSpeakData);
            }

    void speech_SpeakProgress(object sender, System.Speech.Synthesis.SpeakProgressEventArgs e)
            {
                if (speech.Volume != Convert.ToInt32(sliderVolume.Value) || speech.Rate != Convert.ToInt32(sliderRate.Value))
                {
                    speech.Volume = Convert.ToInt32(sliderVolume.Value);
                    speech.Rate = Convert.ToInt32(sliderRate.Value);
                    selectedSpeakData = selectedSpeakData.Remove(0, e.CharacterPosition);
                    speech.SpeakAsyncCancelAll();
                    speech.SpeakAsync(selectedSpeakData);
                }
            }
场罚期间 2024-11-23 23:07:47

当 Slider 控件的值发生变化时,它就会引发一个 ValueChanged 事件。如果您处理此事件,您可以通过检查 Value 属性来更新您的语音音量。

The Slider control raises an event ValueChanged whenever its value changes. If you handle this event you could update your speech volume from there by checking the Value property.

梦情居士 2024-11-23 23:07:47

似乎没有内置的方法可以做到这一点。处理 SpeakProgress 事件将使您能够访问CharacterPosition 属性。这将为您提供提示中最后一个单词的开头位置。如果您在下一个空白字符上执行子字符串并将其作为新提示传递,则将从此时开始说出提示的其余部分。如果您愿意,您可以计算读取提示需要多长时间,并使用 AudioPosition 属性获取提示已运行多长时间的 TimeSpan 对象。

There does not appear to be a built-in way of doing this. Handling the SpeakProgress event will give you access to the CharacterPosition property. This gives you position in the prompt at the start of the last word read. If you do a substring on the next white-space character and pass this as a new prompt, the rest of the prompt will be spoken from this point. If you're up to it, you can calculate how long a prompt will take to be read and use the AudioPosition property to get a TimeSpan object for how long the prompt has been running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文