5

I'm trying to detect if the headphones are plugged in on Windows. I've already tried DirectSound and the NAudio library and have not had any success. Does anyone have any suggestions?

NOTE: this question is a duplicate of this question, but the answer to that question didn't contain any code samples or tutorials.

Community
  • 1
  • 1
Izzy Rodriguez
  • 2,097
  • 3
  • 21
  • 32
  • please pardon my edit of your question, but I wanted to make sure it isn't closed as a duplicate. – MusiGenesis Apr 03 '11 at 02:09
  • Are you trying to detect analog headphones plugged into the line-out jack of a soundcard or USB headphones? That will heavily influence my answer. – selbie Apr 03 '11 at 05:23
  • @selbie: only analog.. But I think I find the answer.. – Izzy Rodriguez Apr 03 '11 at 12:24
  • 1
    Duplicate of this question: http://stackoverflow.com/questions/861601/detecting-when-head-phones-are-plugged-in That question isn't tagged for Windows, but Larry Osterman is a Microsoft software engineer who's worked on the audio subsystem for years so he knows what he's talking about. He doesn't reference any samples or tutorials of how to do this, so I'm still interested to see if anybody digs anything up in response to this question. – MusiGenesis Apr 03 '11 at 02:07
  • Take a look here http://stackoverflow.com/questions/33872895/detect-if-headphones-are-plugged-in-or-not-via-c-sharp – NoWar Dec 01 '15 at 17:11

2 Answers2

1

So...I think I might found something REALY obscure..

Can any of you guys check if that works? - You MUST have the NAudio library referenced..

var enumerator = new MMDeviceEnumerator();
var d = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
Console.WriteLine(d.Properties[new Guid("46d16a2c-5654-41c0-911e-7860d2bce7ee")].Value.ToString());

This property return's 1 (Plugged) or 0 (Unplugged).. and It's working realy good for me..

UPDATE: Just published the app on CodePlex - iTunesHead

Izzy Rodriguez
  • 2,097
  • 3
  • 21
  • 32
  • I have tried running this project on my PC and that particular GUID does not seem to exist (Null reference), so I think it depends on hardware/drivers. I also tried comparing all the device properties before/after plugging and unplugging headphones and nothing changes. – mbwasi May 28 '12 at 19:17
  • Yeah..that was what I was thinking when I found this GUID, must be a thing only on my Dell, so I kinda drop this project after that.. – Izzy Rodriguez Jun 02 '12 at 01:23
-1

What about Microsoft.DirectX.DirectSound.Speakers?

A Speakers object should have one of the following speaker type properties set to true:

  • FivePointOne
  • Headphone
  • Mono
  • Quad
  • SevenPointOne
  • Stereo
  • Surround
typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • All the prop's retun the same value. I keep plugging/unplugging the headphones, all stay the same.. HeadPhone: False FivePointOne: False Mono: False Quad: False SevenPointOne: False Stereo: True Surround: False – Izzy Rodriguez Apr 03 '11 at 02:13
  • Only other refs I could find are: and – typo.pl Apr 03 '11 at 02:27