开发者

.NET Text To Speech Volume

I am working with a simple Text to Speech application using the System.Speech.Synthesis reference. 开发者_StackOverflow中文版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).


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜