文章目录
前言
本文使用Interop.SpeechLib.dll和System.Speech.dll实现语音自动播报
一、开发前准备
引用 Interop.SpeechLib.dll ,System.Speech.dll UI如下
二、开始
1.遍历播报员
正常电脑会遍历到中美2个播报员
private void Form1_Load(object sender, EventArgs e)
{
SpVoice voice = new SpVoice();
int co = voice.GetVoices(string.Empty, string.Empty).Count;
if (co > 0)
{
List<Info> infoList = new List<Info>();
for (int q = 0; q < co; q++)
{
string voicetype = voice.GetVoices(string.Empty, string.Empty).Item(q).GetDescription();
Info zze = new Info();
zze.Id = voicetype.Split('-')[0].ToString().Trim();
zze.Name = voicetype.Split('-')[1].ToString().Trim();
infoList.Add(zze);
}
comboBox2.DataSource = infoList;
comboBox2.ValueMember = "Id";
comboBox2.DisplayMember = "Name";
comboBox2.SelectedIndex = 0;
}
}
2.点击播报
private void button1_Click(object sender, EventArgs e)
{
string text = richTextBox1.Text;
if (text.Trim().Length == 0)
{
UIMessageTip.ShowWarning("不能阅读空内容!", 1000, true);
return;
}
if (uiButton1.Text == "语音试听")
{
speech = new SpeechSynthesizer();
new Thread(Speak).Start();
uiButton1.Text = "停止试听";
}
else if (uiButton1.Text == "停止试听")
{
speech.SpeakAsyncCancelAll();//停止阅读
uiButton1.Text = "语音试听";
}
}
string zze = "";
private void Speak()
{
speech.Rate = rate;//语速
speech.SelectVoice(voicetypess);//设置播音员
speech.Volume = value;//音量
speech.SpeakAsync(zze);//语音阅读方法
speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
speech.SelectVoiceByHints(VoiceGender.Male);//声音性别
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
zze = richTextBox1.Text.ToString();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
voicetypess = comboBox2.SelectedValue.ToString();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
value = trackBar1.Value ;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
rate = Int32.Parse(comboBox1.Text);
}
/// <summary>
/// 语音阅读完成触发此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
uiButton1.Invoke(new SetTextCallback(SetText), "语音试听", 1);
}
delegate void SetTextCallback(string text, int num);
private void SetText(string text, int num)
{
uiButton1.Text = text;
}
3.生成文件
private void button2_Click(object sender, EventArgs e)
{
string text = richTextBox1.Text;
if (text.Trim().Length == 0)
{
UIMessageTip.ShowError("空内容无法生成!", 1000, true);
return;
}
this.SaveFile(text);
}
/// <summary>
/// 生成语音文件的方法
/// </summary>
/// <param name="text"></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();
UINotifierHelper.ShowNotifier("生成成功!在" + path + "路径中!", UINotifierType.OK, UILocalize.SuccessTitle, false, 3000);
}
4.队列
如果需要自动轮询播报那就建一个队列类,在新建一个线程循环跑就可以了
public class QueueList
{
Queue queuelst = new Queue();
public void addQ(string lag)
{
queuelst.Enqueue(lag);
}
public string getQ()
{
string rtnMsg = "";
if(queuelst.Count>0)
{
rtnMsg = queuelst.Dequeue().ToString();
}
return rtnMsg;
}
}
使用方法如下
bool isSpeakCom = true;//在绑定时间完成时将这个值赋值为true 这样就会读完一条才开始读第二条
void speakLan()
{
while(true)
{
if (isSpeakCom)
{
string lag = queueList.getQ();
if(!string.IsNullOrWhiteSpace(lag))
{
isSpeakCom = false;
speech.Rate = 0;
speech.SelectVoice(voicetypess);//设置播音员(中文)
speech.Volume =Convert.ToInt16(sysCurvol);
speech.SpeakAsync(lag);//语音阅读方法
speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
speech.SelectVoiceByHints(VoiceGender.Male);
}
}
Thread.Sleep(50);
}
}
demo地址如下Demo下载地址
总结
以上就是自己个人的一些见解,欢迎大家留言讨论