C#实现语音朗读功能

第一步:新建项目  TTS(从文本到语音(TextToSpeech))

第二步:添加引用 System.Speech

第三步:主界面以及后台代码

using System;
using System.Globalization;
using System.Linq;
using System.Speech.Synthesis;
using System.Windows.Forms;

namespace TTS
{
public partial class Form1 : Form
{
private SpeechSynthesizer speech = new SpeechSynthesizer();

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

#region 只能读数字和英文
//string text = textBox1.Text;

//if (text.Trim().Length != 0)
//{
// speech.Rate = 5;//语速
// speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
// //speech.SelectVoice("Microsoft Anna"); //英文
// speech.Volume = 100; //音量
// speech.SpeakAsync(textBox1.Text);//语音阅读方法
//}
#endregion

#region 可以读取中文
string phrase = "123我是好人";
SpeechSynthesizer speech = new SpeechSynthesizer();
CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
if (neededVoice == null)
{
phrase = "Unsupported Language";
}
else if (!neededVoice.Enabled)
{
phrase = "Voice Disabled";
}
else
{
speech.SelectVoice(neededVoice.VoiceInfo.Name);
}

speech.Speak(phrase);
#endregion
}
}
}

猜你喜欢

转载自www.cnblogs.com/1175429393wljblog/p/9122135.html
今日推荐