Raj123
Raj123

Reputation: 981

How to check if a microphone is available for recording

I am working on a WPF application where In need to record audio messages from my user. I followed code on here and its working fine. Now the problem is if it is a desktop I am only checking if there is a built in microphone but not if there is any external microphone connected or not. And I also want to know if the user has disabled the microphone. Can you tell me how to check if any external microphone is connected. I need to display a error message if user will not be able to record audio.

Upvotes: 3

Views: 4869

Answers (1)

Sheridan
Sheridan

Reputation: 69959

In order to detect changes in hardware in C#, you can use the WM_DEVICECHANGE message, which Notifies an application of a change to the hardware configuration of a device or the computer.

As I am far from an expert in this area, I'd rather point you towards the Detecting when a microphone is unplugged question here on StackOverflow, rather than try to explain it to you. The accepted answer from that post should help you with detecting when a microphone has been unplugged. From the accepted answer to the linked question:

using System.Runtime.InteropServices;
const int WM_DEVICECHANGE = 0x0219;
// new device is pluggedin
const int DBT_DEVICEARRIVAL = 0x8000; 
//device is removed 
const int DBT_DEVICEREMOVECOMPLETE = 0x8004; 
//device is changed
const int DBT_DEVNODES_CHANGED = 0x0007; 
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_DEVICECHANGE
    {
        //Your code here.
    }
    base.WndProc(ref m);
}

Here are some further links that may help you with your project:

Sound Activated Recorder with Spectrogram in C# from CodeProject
.NET Voice Recorder from Channel 9
.NET Voice Recorder from CodePlex

Upvotes: 1

Related Questions