Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (6 posts)

Have XAudio2 switch output device

Last post 2/12/2014 4:23 AM by Charybdis. 5 replies.
  • 2/11/2014 8:53 PM

    Have XAudio2 switch output device

    So I'm using media foundation to play music, and xaudio2 to play sound effects. When I plug in a new device though, media foundation recognizes it as the primary one and starts playing the music in there, while xaudio2 keeps using the original one. Any suggestions on how I could handle this so that xaudio2 also switches over? If I remember correctly, by default media foundation is initialized with the "console" role, which determines what outputs it picks, and I'd like to have identical behaviour for my sound effects.

    -thanks
  • 2/11/2014 9:56 PM In reply to

    Re: Have XAudio2 switch output device

    Answer
    Reply Quote
    XAudio2 uses WASAPI which doesn't have any 'migration' support. This is something that Media Foundation implements internally.

    There are a couple of cases here to handle, and the detail depend heavily on your own policy for behavior.

    * If the current WASAPI device is lost, you will get an OnCriticalError notification. In this case, you have to release your XAudio2 instance and recreate it with some new device. Likely there is a new default device in the system, although on Windows you can end up with NO audio device at all.

    * You need to detect if a new audio device is available in your app so you can choose to recreate a new XAudio2 instance (or if there wasn't a default device before, create one). For Win32 desktop applications, this is done by calling RegisterDeviceNotification <http://msdn.microsoft.com/en-us/library/windows/desktop/aa363431.aspx and then via the Win32 message WM_DEVICECHANGE, wParam == DBT_DEVICEARRIVAL. For Windows Store apps, you have to use the WinRT DeviceWatcher <http://code.msdn.microsoft.com/windowsapps/Device-Enumeration-Sample-a6e45169>

    I try to simplify this somewhat with DirectXTK for Audio <http://go.microsoft.com/fwlink/?LinkId=248929> but really only for the scenario of no audio device at startup and getting a new default later, or when you lose the active device and want to retry with a new default.
  • 2/11/2014 10:37 PM In reply to

    Re: Have XAudio2 switch output device

    Hey, first off, thanks for the reply.

    Some searching that I did led towards implementing IMMNotificationClient
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd371417%28v=vs.85%29.aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd756609%28v=vs.85%29.aspx

    Haven't tried it yet, but if I just recreate xAudio2 when I receive the OnDefaultDeviceChanged I should be ok, no?

    EDIT: Hah, scratch that, I'm using a pool for my sourceVoices so I have to figure out an elegant way of recreating them...
  • 2/12/2014 2:03 AM In reply to

    Re: Have XAudio2 switch output device

    You have to recreate all XAudio2 voices and restart them. For 'one-shot' sounds, they can usually just pick up naturally. For looped sounds, it's a case of restarting them. If there is some specific timing, you have to restart them 'in-progress'.
  • 2/12/2014 3:30 AM In reply to

    Re: Have XAudio2 switch output device

    Awesome, I recreate the voices, and now it works just as intended (i.e. both music and soundEffects switch to whatever the default device is automatically).
    So, unless IMMNotificationClient has any disadvantages I wouldn't know about vs RegisterDeviceNotification, I guess I'm done, right?

    Thank you for the info, much appreciated.

  • 2/12/2014 4:23 AM In reply to

    Re: Have XAudio2 switch output device

    RegisterDeviceNotification works on Windows XP or later. IMMNotificationClient works on Windows Vista or later. If your target is a Direct3D 11 minimum OS, then that's the better choice.
Page 1 of 1 (6 posts) Previous Discussion Next Discussion