Exemplo n.º 1
0
        private const int SpFlags = 1; //SpeechVoiceSpeakFlags.SVSFlagsAsyn

        /// <summary>
        /// 使用中文语音播放指定文字。
        /// </summary>
        /// <param name="text">要播放的文字</param>
        public void Speak(String text)
        {
            try
            {
                if (SpeechComLib.Instance.SpVoiceCls != null)
                {
                    //设置语言引擎-为兼容win7与win10不设置Voice引擎,系统采用默认引擎
                    //SpeechSoundHelper.SetComProperty("Voice", _spVoiceCls, comboBox1.SelectedValue);
                    //调用Speak 函数,msg 是要播放的文本,1 是异步播放,因为是异步的 com 对象不立刻释放
                    UserComHelper.CallComMethod("Speak", SpeechComLib.Instance.SpVoiceCls, text, SpFlags);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
            //try
            //{
            //    if (String.IsNullOrEmpty(text))
            //    {
            //        return;
            //    }
            //    SpVoice?.Speak(text, SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);
            //}
            //catch (Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show("EX:" + ex.ToString());
            //}
        }
Exemplo n.º 2
0
        /// <summary>
        /// 进行语音播放
        /// </summary>
        /// <param name="VoiceText"></param>
        public void PlaySpeech(string VoiceText)
        {
            string msg = VoiceText;

            if (_spVoiceCls != null && _deTokens != null)
            {
                try
                {
                    Nullable <DictionaryEntry> engineEntry = null;
                    foreach (DictionaryEntry dEntry in _deTokens)
                    {
                        if (dEntry.Value.ToString().IndexOf("Huihui") >= 0)
                        {
                            engineEntry = dEntry;
                        }
                    }

                    if (engineEntry != null)
                    {
                        //设置语言引擎
                        UserComHelper.SetComProperty("Voice", _spVoiceCls, ((DictionaryEntry)engineEntry).Key);
                        //调用Speak 函数,msg 是要播放的文本,1 是异步播放,因为是异步的 com 对象不立刻释放
                        UserComHelper.CallComMethod("Speak", _spVoiceCls, msg, SpFlags);
                    }
                }
                catch
                {
                }
            }
        }
Exemplo n.º 3
0
 private void InitSAPI()
 {
     try
     {
         //创建语音对象朗读用
         _spVoiceCls = UserComHelper.CreateComObject("SAPI.SpVoice");
         if (_spVoiceCls == null)
         {
             throw new Exception(String.Format("请检查系统是否安装了语音播报环境!"));
         }
         else
         {
             //取得SAPI.ISpeechObjectTokens
             _oISpeechObjectTokens = UserComHelper.CallComMethod("GetVoices", _spVoiceCls);
             object r = UserComHelper.GetComPropery("Count", _oISpeechObjectTokens);
             if (r is int)
             {
                 TokensCount = (int)r;
                 if (TokensCount > 0)
                 {
                     //取得全部语音识别对象模块,及名称,以被以后使用
                     _deTokens = new DictionaryEntry[TokensCount];
                     for (int i = 0; i < TokensCount; i++)
                     {
                         //从集合中取出单个 识别对象模块
                         //返回 SAPI.SpObjectToken
                         object oSpObjectToken = UserComHelper.CallComMethod("Item", _oISpeechObjectTokens, i);
                         //取名称
                         string Description = UserComHelper.CallComMethod("GetDescription", oSpObjectToken) as string;
                         //放到 DictionaryEntry 对象中,key 是 识别对象模块,value 是名称
                         _deTokens[i] = new DictionaryEntry(oSpObjectToken, Description);
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
     }
 }