none
SpeechSynthesizer_SpeechRecognitionEngine 在.Net4.0和4.6中运行结果不同 RRS feed

  • 问题

  • 我想使用SpeechSynthesizer和SpeechRecognitionEngine制作一个问答机器人,并打算编写一个SpeechHelper类用来包装这两个类

    但在.NetFramework4.0下写的代码运行在.NetFramework4.6环境下,产生了一些不正常:

    1.在同一个Class中调用Helper中的ReadAloud和Listen方法,不论动态还是静态调用都不能正常朗读或者识别触发SpeechRecognized事件。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Speech.Recognition;
    using System.Speech.Synthesis;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common.Helpers
    {
        public class SpeechReadAloudHelper
        {
    
    
            public void ReadAloud(string ReadContent)
            {
                 using (SpeechSynthesizer synth = new SpeechSynthesizer())
                {
                    synth.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(speak_SpeakStarted);
                    synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(speak_SpeakCompleted);
    
                    synth.SetOutputToDefaultAudioDevice();
                    //synth.Rate = 2; //设置语速,[-10,10]
                    synth.Volume = 100; //设置音量,[0,100]
    
                    if (synth.GetInstalledVoices().FirstOrDefault<InstalledVoice>(
                        x => x.VoiceInfo.Name == "VW Liang") != null)
                    {
                        synth.SelectVoice("VW Liang");
                    }
    
                    synth.SpeakAsync(ReadContent);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void speak_SpeakStarted(object sender, SpeakStartedEventArgs e)
            {
                //throw new NotImplementedException();
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void speak_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
            {
                //throw new NotImplementedException();
            }
    
            public void Listen()
            {
                using (
                SpeechRecognitionEngine recognizer =
                  new SpeechRecognitionEngine(
                    new System.Globalization.CultureInfo("zh-CN")))
                {
                    Choices conmmonds = new Choices();
                    conmmonds.Add(new string[] { "今天星期几","现在几点" });
                    GrammarBuilder gBuilder = new GrammarBuilder();
                    gBuilder.Append(conmmonds);
                    Grammar grammar = new Grammar(gBuilder);
                    recognizer.LoadGrammar(grammar);
                    recognizer.SpeechRecognized +=
                      new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
    
                    recognizer.SetInputToDefaultAudioDevice();
    
                    recognizer.RecognizeAsync(RecognizeMode.Multiple);
                }
            }
    
            private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    请问这是什么原因,是否4.0和4.6的异步有关?如何解决这个问题?

    2.Windows7的环境中除了.NetFramework4.6还需要做什么配置才可以运行这个问答程序?

    2017年11月6日 15:34

全部回复

  • Hi,

    感谢在MSDN论坛发帖。

    因为你的问题涉及到speech开发问题,已经超过该论坛讨论的范围,我将把帖子移到一般性讨论论坛。

    对于speech问题,你可以去下面论坛发帖。

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=SpeechService

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月7日 8:55