遇到音频设备错误。 - 错误代码:0x4

时间:2013-08-08 09:37:24

标签: c# vba exception

我正在开发一个关于C#的语音识别程序(使用Visual Basic 2013),当我编译并试用它时,它适用于第一个命令,但在我讲第二个命令时给了我一个奇怪的例外。 详细说明:

  

System.Speech.dll中出现'System.Speech.Internal.Synthesis.AudioException'类型的第一次机会异常

     

System.Speech.dll中发生了'System.Speech.Internal.Synthesis.AudioException'类型的异常,但未在用户代码中处理

     

其他信息:遇到音频设备错误。 - 错误代码:0x4

我的代码很长,但这是第一个也可能是相关的部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
using System.Xml;
using System.Web;

namespace JarvisTest
{
public partial class Form1 : Form
{
    SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
    SpeechSynthesizer JARVIS = new SpeechSynthesizer();
    string QEvent;
    string ProcWindow;
    double timer = 10;
    int count = 1;
    Random rnd = new Random();

    string Temperature;
    string Condition;
    string Humidity;
    string WindSpeed;
    string Town;
    string TFCond;
    string TFHigh;
    string TFLow;
    string Stuff;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _recognizer.SetInputToDefaultAudioDevice();
        _recognizer.LoadGrammar(new DictationGrammar());
        _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Aristotelis\Documents\JarvisTest.txt")))));
        _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
        _recognizer.RecognizeAsync(RecognizeMode.Multiple);
    }


    void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        int ranNum = rnd.Next(1, 10);

        string speech = e.Result.Text;
        switch (speech)
        {
            //WOLFRAM
            case "ram":               
                WolframAlpha();
                JARVIS.Speak("time for stuff" + Stuff);
                break;

            //WEATHER

            case "hows the weather":
                GetWeather();
                JARVIS.Speak("The temperature in " + Town + " is " + Temperature + " degrees.");
                break;

            //GREETINGS
            case "hello":
            case "hello jarvis":
                if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
                else if (ranNum > 5) { JARVIS.Speak("Hi"); }
                break;

我不确定导致问题的原因。任何帮助都非常感谢,提前感谢。

堆栈跟踪显示:

Expression: ((System.Speech.Internal.Synthesis.AudioException)$exception).StackTrace
Value:
at System.Speech.Internal.Synthesis.VoiceSynthesis.Speak(Prompt prompt)
at System.Speech.Synthesis.SpeechSynthesizer.Speak(Prompt prompt)
at System.Speech.Synthesis.SpeechSynthesizer.Speak(String textToSpeak)
at JarvisTest.Form1._recognizer_SpeechRecognized(Object sender, SpeechRecognizedEventArgs e) in c:\Users\Aristotelis\Documents\Visual Studio 2012\Projects\JarvisTest\JarvisTest\Form1.cs:line 77
at System.Speech.Recognition.SpeechRecognitionEngine.SpeechRecognizedProxy(Object sender, SpeechRecognizedEventArgs e)

1 个答案:

答案 0 :(得分:0)

当您的语音设备损坏时,通常会发生此类错误。 你见过http://social.msdn.microsoft.com/Forums/vstudio/en-US/9075023b-8bc4-4632-a573-77470b403a48/i-get-an-audioexception-0x2 ???