C# 语音技术
1、使用DotNetSpeech.dll。
/// <summary>
/// 朗读
/// </summary>
/// <param name="text">要朗读的文本</param>
private void Read(string text)
{
SpVoice sv = new SpVoice();
sv.Rate = 0;//设置朗读速度
SpeechVoiceSpeakFlags SSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
sv.Speak(text, SSF);
}
/// <summary>
/// 生成声音文件
/// </summary>
/// <param name="text">要朗读的文本</param>
/// <param name="filePath">生成声音文件的路径</param>
/// <param name="fileName">生成声音文件的名称</param>
private void CreateFile(string text, string filePath,string fileName)
{
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
SpVoice sv = new SpVoice();
SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SFS = new SpFileStream();
SFS.Open(filePath+fileName, SSFM, false);
sv.AudioOutputStream = SFS;
sv.Speak(text, SVSF);
sv.WaitUntilDone(System.Threading.Timeout.Infinite);
SFS.Close();
}
/// </summary>
/// <param name="text">要朗读的文本</param>
private void Read(string text)
{
SpVoice sv = new SpVoice();
sv.Rate = 0;//设置朗读速度
SpeechVoiceSpeakFlags SSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
sv.Speak(text, SSF);
}
/// <summary>
/// 生成声音文件
/// </summary>
/// <param name="text">要朗读的文本</param>
/// <param name="filePath">生成声音文件的路径</param>
/// <param name="fileName">生成声音文件的名称</param>
private void CreateFile(string text, string filePath,string fileName)
{
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
SpVoice sv = new SpVoice();
SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SFS = new SpFileStream();
SFS.Open(filePath+fileName, SSFM, false);
sv.AudioOutputStream = SFS;
sv.Speak(text, SVSF);
sv.WaitUntilDone(System.Threading.Timeout.Infinite);
SFS.Close();
}
2、 使用System.Speech
SpeechSynthesizer ss = new SpeechSynthesizer();//播放
if (ss != null)
{
ss.Dispose();
ss.SpeakAsync("朗读的文本");
}
//暂停
if (ss.State == SynthesizerState.Speaking)
{
ss.Pause();
}
//继续
if (reader.State == SynthesizerState.Paused)
{
ss.Resume();
}
//停止
if (ss != null)
{
ss.Dispose();
}
作者:sy
出处:http://www.cnblogs.com/sydeveloper
QQ:522733724
本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利