注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

午夜阳光

 
 
 

日志

 
 

c#TTS语音识别System.Speech.ll和DotNetSpeech.dll实现  

2014-09-05 10:13:36|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1、System.Speech方式

1.1 引用System.Speech:添加引用》.Net》System.Speech

1.2 代码

using System.Speech.Synthesis;

namespace TTSTest
{
    public class SystemSpeecher
    {
        SpeechSynthesizer synth;
        public SystemSpeecher()
        {
            synth = new SpeechSynthesizer();
            synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);
        }

        void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            Console.WriteLine("UserState:{0}",e.UserState);
            Console.WriteLine("是否取消:{0}",e.Cancelled);
            Console.WriteLine("错误信息:{0}", e.Error);
            Console.WriteLine("Prompt:{0}", e.Prompt!=null?e.Prompt.ToString():null);
        }
        public void Speak(string text)
        {
            try
            {
                //synth.Speak(text);
                synth.SpeakAsync(text);

            }
            catch (Exception ex)
            {
               Console.WriteLine(ex.Message);
            }
        }
        public void Stop()
        {
            try
            {                
                //synth.Speak(string.Empty);
                synth.SpeakAsyncCancelAll();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
             }
        }
    }
}

2、DotNetSpeech.dll方式

2.1 引用DotNetSpeech.dll,嵌入互操作类型改成false,复制到本地改成true

2.2 代码

using DotNetSpeech;
using Microsoft.Win32;

namespace IMCP.iShow.GUI.UserCtrl
{
    public  class AlarmBroadcaster
    {
        public delegate void EndSpeakHanlder();
        public  event EndSpeakHanlder EndSpeakEvent=null;
        SpVoice voice = new SpVoice();
        public AlarmBroadcaster()
        {
            try
            {
                voice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(voice_EndStream);
            }
            catch (Exception ex)
            {            }
        }
        public  void Stop()
        {
            try
            {
                voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
            }
            catch (Exception ex)
            {
                ImcpLog.WrtieExceptionLog("AlarmBroadcaster", "Stop", ex.Message, ex.StackTrace);
            }
        }    

         void voice_EndStream(int StreamNumber, object StreamPosition)
        {
            try
            {
                if (EndSpeakEvent != null)
                {
                    EndSpeakEvent();
                }
            }
            catch (Exception ex)
            {                     }
        }
        public  void Pause()
        {
            try
            {
                voice.Pause();//暂停,使用
            }
            catch (Exception ex)
            {  }
        }
        //从暂停中继续刚才的朗读,使用
        public  void Restart()
        {
            try
            {
                voice.Resume();
            }
            catch (Exception ex)
            {}
        }
        public  int SpeakText(string text)
        {
            try
            {
                //sp.Voice = sp.GetVoices("name=Microsoft Simplified Chinese", "").Item(0);
                //Voice中是语音类型(语言、男(女)声),有 Microsoft Simplified Chinese,Microsoft Mary(Sam,Mike)等,
                //也可以这样:voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
                voice.Volume = 100;
                voice.Rate = -2;//朗读语速
                return voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync); //SpeechVoiceSpeakFlags是语音朗读的风格     
            }
            catch (Exception ex)
            {
                      return -1;
            }
        }
        //生成语音文件
        public void GenerateVoiceFile(string text)
        {
            try
            {
                SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;

                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
                dialog.Title = "保存WAV文件";
                dialog.FilterIndex = 2;
                dialog.RestoreDirectory = true;
                if (dialog.ShowDialog() == true)
                {
                    SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
                    SpFileStream spFileStream = new SpFileStream();
                    spFileStream.Open(dialog.FileName, spFileMode, false);
                    voice.AudioOutputStream = spFileStream;
                    voice.Speak(text, spFlags);
                    voice.WaitUntilDone(1000);
                    //上面两句一定要写上,否则产生的文件没有声音
                    //WaitUntilDone的后面的smTimeout是一个int型
                    spFileStream.Close();
                }
            }
            catch (Exception ex)
            { }
        }
    }
}

  评论这张
 
阅读(345)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017