.NET文本到语音卷 [英] .NET Text To Speech Volume

查看:125
本文介绍了.NET文本到语音卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用System.Speech.Synthesis参考的一个简单的Text to Speech应用程序。我想给应用程序添加一个滑块控件,并用它控制语音的音量。为了设置我使用的音量:

  speech.Volume = 100; 

为了更新此值,我需要使用某种事件处理程序吗?顺便说一下,我正在使用C#创建一个WPF应用程序(请不要使用VB.NET代码)。

解决方案

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

  string selectedSpeakData =示例文本示例文本示例文本示例文本示例文本; 
私人SpeechSynthesizer演讲;

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(sli​​derVolume。值)|| speech.Rate!= Convert.ToInt32(sli​​derRate.Value))
{
speech.Volume = Convert.ToInt32(sli​​derVolume.Value);
speech.Rate = Convert.ToInt32(sli​​derRate.Value);
selectedSpeakData = selectedSpeakData.Remove(0,e.CharacterPosition);
speech.SpeakAsyncCancelAll();
speech.SpeakAsync(selectedSpeakData);
}
}


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).

解决方案

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);
                }
            }

这篇关于.NET文本到语音卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆