This project has moved. For the latest updates, please go here.

Plays in speakers, but not in headphones

Jan 10, 2012 at 6:21 PM

Hello - I'm using WaveOut with all the defaults. I get a .wav file to play fine when I have no headphones plugged in, but when I have them plugged in I get nothing. I'm sure it's something simple I'm missing...

Thanks!

Jan 10, 2012 at 7:07 PM

Update - so there's more to the story.

If I start the program with the headphones plugged in, then unplug them before I tell it to play, I get the sound coming through the speakers (but never the headphones).

If I start the program with the headphones unplugged, then plug them in before I tell it to play, the sound comes through the headphones (but never the speakers).

Is there something that I need to set manually so it uses the correct audio device???

Jan 11, 2012 at 12:10 PM

Your laptop probably has the automatic headphone detection, which is causing problems. You might try to manually select the device before playing the file with using the .DeviceNumber property of your waveOut device corresponding to your desired output.

Dim waveOutDevice As WaveOut
waveOutDevice.DeviceNumber = 0
0 is dafault audio device. 1 is secondary etc. You have to do that before the Init() though.

Jan 11, 2012 at 3:51 PM

Thanks for the reply.

I have done as you suggested, but the effect is the same. I've tried setting it to 0, 1, & -1 but I get nothing through headphones or speakers.

It was done immediately after I created the device and before Init() .... any other ideas?

Coordinator
Jan 11, 2012 at 3:54 PM

are they USB headphones, in which case Windows could be treating it as a new audio device

Jan 11, 2012 at 4:13 PM

I have actually tested with both USB and regular and have the same result.

Coordinator
Jan 11, 2012 at 4:14 PM

what OS are you using? Win 7 is supposed to do clever stuff under the scenes so this type of thing ought to just work.

Jan 11, 2012 at 4:51 PM

Yeah, it's really confusing me..I hope I'm not missing something really simple here.

I am using 7 (Ultimate). I just don't understand why it would work in the speakers only AFTER I unplug the headphones...

Just to be clear, this is the order I am doing everything:

1.  Create Device (using all defaults)

2.  Set DeviceNumber (tried 0,1,-1), or just leave at default

3.  Create WaveStream with WaveChannel32 (or just WaveStream), using WaveFileReader and set appropriate WaveFormat

4.  Init WaveOut with the WaveStream

5.  call Play()

If I want to get sound through the speakers, I have to have the headphones plugged in until before I call Init and Play.

If I want to get sound through headphones, I have to have the headphones unplugged until before I call Init and Play...

It's really bizarre...and I have been using a different sound API which works just fine, so it doesn't seem to be something weird with my PC.

Coordinator
Jan 11, 2012 at 4:52 PM

does it do the right thing when say Windows Media Player or a web page are playing sound and you change the device?

Also, have you tried with DirectSoundOut and see if that works?

Jan 11, 2012 at 4:58 PM

Yeah it works correctly with WMP.

I haven't tried DirectSoundOut, I'll give that a shot.

Jan 11, 2012 at 5:04 PM

Yeah the DirectSoundOut works fine...weird.

You mentioned in your blog that it's pretty widely supported so I'm sure this will work just fine.

Alright, well thanks! 

Jan 25, 2012 at 9:15 AM

I am having the similar problem if switching audio devices. You have to detect if any audio device (endpoint from now on) is added (enabled) or removed (disabled) and redirect the audio to the new default device. Unfortunately NAudio seems not to do it automatically at this time as your case proves.

The Windows 7 audio subsystem (default audio endpoint selection) were rewritten. This means that an audio endpoints has priority over the other. For example if you plug-in a bluetooth headphones they will be set as the default audio endpoint. Even if you have other wired pair headset connected.

The point is that Windows's audio system developers want to prohibit audio applications to set default endpoint. So Windows user could control the default device directly from the control panel. Anyway this is a bit of off-topic now.


Windows 7 determines a default audio endpoint for any specific role:

  • The audio subsystem loops through all the ACTIVE endpoints with a matching device role to find the endpoint with the most recent timestamp property. The timestamp property indicates the last time an endpoint was set as the default audio endpoint.
  • If such an audio endpoint is found, it is returned as the default audio endpoint.
  • Otherwise, the audio subsystem resorts to the system default audio endpoint heuristic algorithm to determine the default audio endpoint.

The Windows 7 Default Audio Endpoint Heuristic:

  • When the heuristic selects the default communications endpoint, it assigns higher priority to Bluetooth headphone endpoints than to onboard HD Audio headphones.
  • When it selects the default speaker endpoint, it assigns higher priority to speaker endpoints that have jack detection capability than to static speaker endpoints.
  • When it selects the default capture device, it assigns higher priority to front microphone endpoints than to microphones in the rear of a computer.
  • When it selects the default console render endpoint, it assigns higher priority to S/PDIF endpoints than to handset endpoints. When it selects the default communications render endpoint, it assigns higher priority to handset endpoints than to S/PDIF endpoints.
  • The heuristic can designate an endpoint that has specific characteristics to be the top priority or the lowest priority. For example, it can make a jack detection capable HD Audio headphone endpoint on the right of a computer be the highest priority default communication render device.

These were taken from "Default Audio Endpoint Selection in Windows 7" DOCX at http://msdn.microsoft.com/en-us/windows/hardware/gg463052.aspx where you could find some additional information that might interest you.

Aug 13, 2016 at 5:33 PM
Hi,

is there yet a solution for switching the "default audio device" in Windows?
I'm now having the same problem.

Once I have created a naudio WaveOut object and the users switches the windows audio device, the sound still is played on the old device.

Regards
Der_Knob