标签:pre hub online 第三方 www. code blank 一个 log
原文:C# 语音合成1. 引用System.Speech
2. 通过SpeechSynthesizer类朗读文本
new SpeechSynthesizer().SpeakAsync("我们都是好孩子We‘re good kids.")
3. Speck vs SpeckAsync函数
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. 设置朗读角色
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. 其它
6. 导出音频文件
可以将文本语音合成后,导出成一个wav、mp3等音频文件。
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,第三方的语音合成接口有:
如果是英文朗读的话,有道的效果最好。
标签:pre hub online 第三方 www. code blank 一个 log
原文地址:https://www.cnblogs.com/lonelyxmas/p/10597516.html