进行语音合成和播放,其中语音合成使用的是微软的TTS:
Object strbuffer = new IntPtr(); SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;//异步朗读 SpVoice voice = new SpVoice(); voice.Rate = -3;//语速 SpMemoryStream spmem = new SpMemoryStream();//内存流 spmem.Format.Type = SpeechAudioFormatType.SAFTCCITT_ALaw_8kHzMono;//语音格式为A-law 8KHz voice.AudioOutputStream = spmem;//输出到内存中 voice.Speak(InvName, SpFlags); voice.WaitUntilDone(50000);//等待完成或者超时 strbuffer = spmem.GetData();//获取内存中的语音内容返回的strbuffer为object类型,而播放函数StartPlay(chnnelno i,byte[] content,int startposition , uint LengthOfBytes)中播放内容content类型为byte[],所以需要进行类型转换。
转换代码如下:
BinaryFormatter se = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); se.Serialize(memStream, strbuffer); byte[] bobj = memStream.ToArray(); memStream.Close();
//播放 uint length = (uint)bobj.GetLength(0);//获取数组长度 Tc08a32.StartPlay(i, bobj, 0,length);//播放函数
注:
需要添加命名空间的引用:
using System.IO; using System.Runtime.Serialization.Formatters.Binary;