C# 語音合成

1. 引用System.Speechgit

2. 經過SpeechSynthesizer類朗讀文本github

new SpeechSynthesizer().SpeakAsync("咱們都是好孩子We're good kids.")異步

3. Speck vs SpeckAsync函數函數

  • PlayAsync--異步播放,能夠將須要朗讀的文本進行排隊。若是不須要,能夠按以下取消當前的播放操做。
  • Speak--同步播放,會卡UI線程。若是在朗讀時,界面沒有其它操做,則可使用此函數
 1         private SpeechSynthesizer speechSyn=new SpeechSynthesizer();
 2         /// <summary>
 3         /// 異步播放
 4         /// </summary>
 5         private void PlayAsync()
 6         {
 7             var currentSpokenPrompt = speechSyn.GetCurrentlySpokenPrompt();
 8             if (currentSpokenPrompt != null)
 9             {
10                 speechSyn.SpeakAsyncCancel(currentSpokenPrompt);
11             }
12             speechSyn.SpeakAsync(richTextBox1.Text);
13         }
14         /// <summary>
15         /// 同步播放
16         /// 注:卡UI
17         /// </summary>
18         private void Play()
19         {
20             using (SpeechSynthesizer speechSyn = new SpeechSynthesizer())
21             {
22                 speechSyn.Speak(richTextBox1.Text);
23             }
24         }

4. 設置朗讀角色spa

1     var speechSynthesizer = new SpeechSynthesizer();
2     var voices= speechSynthesizer.GetInstalledVoices(CultureInfo.CurrentCulture).Select(x => x.VoiceInfo.Name).ToList();
3     speechSynthesizer.SelectVoice(voices[0]);
4     speechSynthesizer.SpeakAsync("咱們都是好孩子We're good kids.");

5. 其它線程

  • Rate -- 語速設置,默認爲0
  • Volume -- 音量設置

6. 導出音頻文件code

能夠將文本語音合成後,導出成一個wav、mp3等音頻文件。blog

 1         private void ExportAudioFile()
 2         {
 3             using (SpeechSynthesizer speechSyn = new SpeechSynthesizer())
 4             {
 5                 speechSyn.Volume = 50;
 6                 speechSyn.Rate = 0;
 7 
 8                 var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + $"\\{richTextBox1.Text}.mp3";
 9                 if (File.Exists(filePath))
10                 {
11                     File.Delete(filePath);
12                 }
13 
14                 speechSyn.SetOutputToWaveFile(filePath);
15                 speechSyn.Speak(richTextBox1.Text);
16                 speechSyn.SetOutputToDefaultAudioDevice();
17 
18                 MessageBox.Show($"保存錄音文件成功,保存路徑:{filePath}");
19             }
20         }

Demo下載接口

 

PS,第三方的語音合成接口有:ci

若是是英文朗讀的話,有道的效果最好。

相關文章
相關標籤/搜索