VB.Net - How to detect if a microphone is connected to Mic or Line-in port

I have an audio recording application written in VB.Net and want to check to make sure the microphone is connected before recording starts.

The microphone is usually connected to the microphone or Line-in jack.

How can I detect whether a device is connected to a line-in or a mic-in port?
Jim LittleVP Technology & DevelopmentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi  jrlittle86;

Never had the need to do this but there is documentation on this, Audio Endpoint Devices, See if this helps you through your issue.
Jim LittleVP Technology & DevelopmentAuthor Commented:
Thanks for the direction.  

Starting from there I was able to find this:  http://stackoverflow.com/questions/1525320/how-to-enumerate-audio-out-devices-in-c-sharp .  It's in C# but can be easily converted to VB.Net using a converter (if you need one).

Turns out you can enumerate audio devices within the system.management namespace and check for the availability of your device within the list.

From there we only needed to check to see if the enumerated device is available.  If it is the function will return TRUE (and FALSE if the device is unavailable).

Here's a modification of the code:

    Public Function GetAudioDevices(AudioDeviceName as String) as Boolean

        Dim objSearcher As New ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice")
        Dim objCollection As ManagementObjectCollection = objSearcher.[Get]()
        Dim DeviceAvailable as Boolean = False
       
        For Each obj As ManagementObject In objCollection

            For Each [property] As PropertyData In obj.Properties

                If [property].Name = AudioDeviceName Then
                      DeviceAvailable = True
                End If

            Next

        Next

        Return DeviceAvailable

    End Sub

It's not perfect but it's a start.  Even though a microphone or some other audio source may be plugged into the Line In port, it's possible the mic itself isn't turned on and you're getting no sound.  That is another issue and a separate topic altogether.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim LittleVP Technology & DevelopmentAuthor Commented:
Fernando was able to point me in the direction where to start looking.  That was helpful in tracking down an answer but didn't directly provide the code or algorithm I needed.  I thought it would be helpful to include that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.