简体   繁体   中英

Audio device error encountered. - Error Code: 0x4

I'm working on a speech recognition program on C# (using Visual Basic 2013), and when I compile and try it out, it works fine for the first command but gives me a weird exception when I speak the second command. Details:

A first chance exception of type 'System.Speech.Internal.Synthesis.AudioException' occurred in System.Speech.dll

An exception of type 'System.Speech.Internal.Synthesis.AudioException' occurred in System.Speech.dll but was not handled in user code

Additional information: Audio device error encountered. - Error Code: 0x4

My code is quite a few lines long, but here's the first and possibly related part:

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;

I'm not sure what causes the problem. Any help is highly appreciated, thanks in advance.

Stack tracing shows:

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)

This type of error generally occurs when your speech device is damaged. Have you seen http://social.msdn.microsoft.com/Forums/vstudio/en-US/9075023b-8bc4-4632-a573-77470b403a48/i-get-an-audioexception-0x2 ???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM