本帖最后由 milian91 于 2012-10-26 13:08:20 编辑

解决方案 »

  1.   

    未处理 System.ArgumentException
      Message=值不在预期的范围内。
      Source=System.Speech
      StackTrace:
           在 System.Speech.Internal.SapiInterop.SapiProxy.MTAThread.Invoke2(VoidDelegate pfn)
           在 System.Speech.Internal.SapiInterop.SapiRecognizer.SetInput(Object input, Boolean allowFormatChanges)
           在 System.Speech.Recognition.RecognizerBase.SetInputToDefaultAudioDevice()
           在 System.Speech.Recognition.SpeechRecognitionEngine.SetInputToDefaultAudioDevice()
           在 Speech.Form1.Form1_Load(Object sender, EventArgs e) 位置 c:\documents and settings\administrator\my documents\visual studio 2010\Projects\Speech\Speech\Form1.cs:行号 36
           在 System.Windows.Forms.Form.OnLoad(EventArgs e)
           在 System.Windows.Forms.Form.OnCreateControl()
           在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           在 System.Windows.Forms.Control.CreateControl()
           在 System.Windows.Forms.Control.WmShowWindow(Message& m)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.Form.WmShowWindow(Message& m)
           在 System.Windows.Forms.Form.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
           在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
           在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
           在 System.Windows.Forms.Control.set_Visible(Boolean value)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 Speech.Program.Main() 位置 c:\documents and settings\administrator\my documents\visual studio 2010\Projects\Speech\Speech\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  2.   

    win7下,vs2008,用过一个C#的,忘了在什么地方下的了。
    用法:
    SRecognition sr;
    string[] fg = { "东方", "西方", "南方", "北方" };
    sr = new SRecognition(fg);
    sr.BeginRec(textBox1);// 开始,textBox1输出结果
    //sr.over();//停止
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Speech;
    using System.Speech.Recognition;
    using System.Globalization;
    using System.Windows.Forms;namespace IME
    {
      public class SRecognition
      {
        public SpeechRecognitionEngine recognizer = null;//语音识别引擎  
        public DictationGrammar dictationGrammar = null; //自然语法  
        public System.Windows.Forms.Control cDisplay; //显示控件      public SRecognition(string[] fg) //创建关键词语列表  
        {
          CultureInfo myCIintl = new CultureInfo("zh-CN");
          foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//获取所有语音引擎  
          {
            if (config.Culture.Equals(myCIintl) && config.Id == "MS-2052-80-DESK")
            {
              recognizer = new SpeechRecognitionEngine(config);
              break;
            }//选择识别引擎
          }
          if (recognizer != null)
          {
            InitializeSpeechRecognitionEngine(fg);//初始化语音识别引擎  
            dictationGrammar = new DictationGrammar();
          }
          else
          {
            MessageBox.Show("创建语音识别失败");
          }
        }
        private void InitializeSpeechRecognitionEngine(string[] fg)
        {
          recognizer.SetInputToDefaultAudioDevice();//选择默认的音频输入设备  
          Grammar customGrammar = CreateCustomGrammar(fg);
          //根据关键字数组建立语法  
          recognizer.UnloadAllGrammars();
          recognizer.LoadGrammar(customGrammar);
          //加载语法  
          recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
          //recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);  
        }
        public void BeginRec(Control tbResult)//关联窗口控件  
        {
          TurnSpeechRecognitionOn();
          TurnDictationOn();
          cDisplay = tbResult;
        }
        public void over()//停止语音识别引擎  
        {
          TurnSpeechRecognitionOff();
        }
        public virtual Grammar CreateCustomGrammar(string[] fg) //创造自定义语法  
        {
          GrammarBuilder grammarBuilder = new GrammarBuilder();
          grammarBuilder.Append(new Choices(fg));
          return new Grammar(grammarBuilder);
        }
        private void TurnSpeechRecognitionOn()//启动语音识别函数  
        {
          if (recognizer != null)
          {
            recognizer.RecognizeAsync(RecognizeMode.Multiple);
            //识别模式为连续识别  
          }
          else
          {
            MessageBox.Show("创建语音识别失败");
          }
        }
        private void TurnSpeechRecognitionOff()//关闭语音识别函数  
        {
          if (recognizer != null)
          {
            recognizer.RecognizeAsyncStop();
            TurnDictationOff();
          }
          else
          {
            MessageBox.Show("创建语音识别失败");
          }
        }
        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
          //识别出结果完成的动作,通常把识别结果传给某一个控件  
          string text = e.Result.Text;
          cDisplay.Text += text;
        }
        private void TurnDictationOn()
        {
          if (recognizer != null)
          {
            recognizer.LoadGrammar(dictationGrammar);
            //加载自然语法  
          }
          else
          {
            MessageBox.Show("创建语音识别失败");
          }
        }
        private void TurnDictationOff()
        {
          if (dictationGrammar != null)
          {
            recognizer.UnloadGrammar(dictationGrammar);
            //卸载自然语法  
          }
          else
          {
            MessageBox.Show("创建语音识别失败");
          }
        }
      }
    }
      

  3.   

    整理成一份最简单的。vs2008编译,win7下,运行正常。新建项目需要引用.net的System.Speechhttp://files.cnblogs.com/slowhand/SR.zip
      

  4.   

    我测试过了,win7下能运行。。
    控制台程序能在win2003运行,但是winFrom程序不能,会出现
    这个错误。
    未处理 System.ArgumentException
      Message=值不在预期的范围内。
      Source=System.Speech
      

  5.   

    木有2003环境,囧。目测这个System.Speech应该是.net FrameWork3.5 里面自带的
      

  6.   

    在win2003环境下运行就要报错,但是在win7下就不能正常运行。。但是MSDN上面的实例在控制台程序能在2003运行。我不清楚是哪个环节出问题了。