user3342380
user3342380

Reputation: 31

No microphone? SpeechRecognitionEngine SetInputToDefaultAudioDevice() problems

I'm working on a C# WPF application in VS 2012 that allows for speech input. I use the SpeechRecognitionEngine rather than SpeechRecognizer because the latter has some conflicts between the application and the Windows Recognizer. It works wonderfully when a microphone is plugged in. However, once there is no microphone (I've been testing this on a public computer that has any non-wired audio I/O disabled), the program does not execute properly: virtually all code after the SetInputToDefaultAudioDevice method does not run. I've tested this with the following code:

    private void NewWindowLoad(object sender, RoutedEventArgs e)
    {
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();

        // Accept input from the default audio device
        sre.SetInputToDefaultAudioDevice();
        MessageBox.Show("HELLO");
    }

In this instance, no MessageBox shows up. Obviously, this code is stripped to the bare bones - in reality, the application actually does something with speech...that's not the problem, though.

I don't get any errors or exceptions from running the method. The only exception that occurs is a later call the SpeechRecognitionEngine's RecognizeAsync() method. Is there a way to check there is actually a usable audio input device prior to running this code? Alternatively, is there another way to go about doing this? The application should be able to run on any supported OS where audio input may or may not be available.

Upvotes: 3

Views: 2240

Answers (1)

Eric Brown
Eric Brown

Reputation: 13942

waveInGetNumDevs will return the number of wave input devices. If it's zero, there are no audio input devices. This doesn't have a direct C# binding, but the P/Invoke is pretty trivial:

[DllImport("winmm.dll")]
public static extern int waveInGetNumDevs();

Upvotes: 1

Related Questions