问题
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).
回答1:
<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;
回答2:
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);
}
}
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/6444081/net-text-to-speech-volume