1. 程式人生 > >C# 音訊播放示例程式碼

C# 音訊播放示例程式碼

庫檔案:

使用VS自帶的speech或者DotNetSpeech

程式碼實現:

基本實現:

using DotNetSpeech;

SpVoice speech = new SpVoice();
speech.Rate = 1;
speech.Volume = 100

speech.Speak("文字檔案", SpeechVoiceSpeakFlags.SVSFlagsAsync);

DotNetSpeech使用詳解:

介面:

主要功能:

1.獲取語音引擎以及音訊輸出方式

//初始化語音引擎列表
foreach (ISpeechObjectToken Token in speech.GetVoices(string.Empty, string.Empty))
{
    cmbVoices.Items.Add(Token.GetDescription(49));
}
//取得音訊輸出列表
foreach (ISpeechObjectToken AudioOut in speech.GetAudioOutputs(string.Empty, string.Empty))
{
    cmbAudioOut.Items.Add(AudioOut.GetDescription(49));
}

2.調節音量以及語速

private void cmbVoices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    speech.Voice = speech.GetVoices(string.Empty,     string.Empty).Item(cmbVoices.SelectedIndex);
}

private void cmbAudioOut_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    speech.AudioOutput = speech.GetAudioOutputs(string.Empty, string.Empty).Item(cmbAudioOut.SelectedIndex);
}

private void tbarRate_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    speech.Rate = (int)e.NewValue;
}

private void trbVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    speech.Volume = (int)e.NewValue;
}

3.朗讀文字

  speech.Speak(tbspeech.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);

示例程式碼