1. 利用DONET框架自带的 SpeechSynthesizer ,缺点是没有感情色彩,抑扬顿挫等。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Speech.Synthesis; using System.Threading; using Microsoft.Win32; namespace MSSpeech { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); speech = new SpeechSynthesizer(); speech.Rate = rate; // speech.SelectVoice("Microsoft Lili");//设置播音员(中文) speech.SelectVoice("Microsoft Anna"); //英文 speech.Volume = value; speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件 Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing); } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { speech.SpeakAsyncCancelAll();//停止阅读 speech.SpeakCompleted -= speech_SpeakCompleted; } private SpeechSynthesizer speech; /// <summary> /// 音量 /// </summary> private int value = 100; /// <summary> /// 语速 /// </summary> private int rate; private string words = ""; private void btnSpeech_Click(object sender, RoutedEventArgs e) { test(); } void test() { string text = textBox1.Text; if (text.Trim().Length == 0) { MessageBox.Show("不能阅读空内容!", "错误提示"); return; } words = textBox1.Text; new Thread(Speak).Start(); } private void Speak() { speech.SpeakAsync(words);//语音阅读方法 } /// <summary> /// 语音阅读完成触发此事件 /// </summary> /// <param></param> /// <param></param> void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { btnSpeech.Content = "语音试听"; } // <summary> /// 生成语音文件的方法 /// </summary> /// <param></param> private void SaveFile(string text) { speech = new SpeechSynthesizer(); var dialog = new SaveFileDialog(); dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3"; dialog.ShowDialog(); string path = dialog.FileName; if (path.Trim().Length == 0) { return; } speech.SetOutputToWaveFile(path); speech.Volume = value; speech.Rate = rate; speech.Speak(text); speech.SetOutputToNull(); MessageBox.Show("生成成功!在" + path + "路径中!", "提示"); } } }