使用 System.Speech 可以很容易实现TTS。
原代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Speech.Synthesis; using System.Text; using System.Windows.Forms; namespace ttsTest { public partial class Form1 : Form { private SpeechSynthesizer synthes = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { synthes = new SpeechSynthesizer(); trackBarVolume.Value = synthes.Volume; trackBarRate.Value = synthes.Rate; lblVolume.Text = "音量:" + trackBarVolume.Value; lblRate.Text = "语速:" + trackBarRate.Value; cmbVoice.BeginUpdate(); cmbVoice.Items.Clear(); foreach (var r in synthes.GetInstalledVoices()) { if (r.Enabled) { cmbVoice.Items.Add(r.VoiceInfo.Name); } } cmbVoice.EndUpdate(); cmbVoice.SelectedItem = synthes.Voice.Name; } private void btnPlay_Click(object sender, EventArgs e) { string str = txtContent.Text.Trim(); if (string.IsNullOrEmpty(str)) { return; } synthes.SpeakAsyncCancelAll(); synthes.SelectVoice(cmbVoice.SelectedItem.ToString()); // synthes.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior, 2, System.Globalization.CultureInfo.CurrentCulture); synthes.Volume = trackBarVolume.Value; synthes.Rate = trackBarRate.Value;//-10-10,值越小,语速越慢 // synthes.Speak(txtContent.Text);//同步 synthes.SpeakAsync(str);//异步 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { synthes.SpeakAsyncCancelAll(); } private void trackBarVolume_Scroll(object sender, EventArgs e) { lblVolume.Text = "音量:" + trackBarVolume.Value; } private void trackBarRate_Scroll(object sender, EventArgs e) { lblRate.Text = "语速:" + trackBarRate.Value; } } }