[SlimDX/Xaudio2] Playing when application loses focus

Started by
2 comments, last by turnpast 14 years, 1 month ago
I want Xaudio2 to keep playing even when the application window has lost focus. How do I do that? DirectSound has the DSBCAPS_GLOBALFOCUS to get things done but I can't find a similar way to make Xaudio2 do the job. Any ideas anyone?
Advertisement
I was not aware that it did this. Make sure you are not using any windows forms events to drive the audio (including the windows forms timer). If you are using Application.Run to start your GUI you might consider using a Show call on the primary form instead and, keeping your main thread in your hands and creating all your XAudio2 objects from it rather than winforms callbacks etc.
When the window loses focus, my application actually continues running in the background feeding sound buffers to Xaudio2 to play. So if there's some music playing when the app was in foreground, it will continue to play even if app loses focus.

I'm trapping the BufferEnd event in my sound manager class (which also instantiates the audio device) to let me know when a buffer has finished playing. I suspect that this event isn't getting triggered when app goes to the background and as a result I never get a BufferEnd event thereby stalling the app. Unfortunately, I can't reproduce this bug in Debug mode, only Release mode so can't trace it proper.

turnpast, from my understanding of what you said, I would need my main form to instantiate the audio device so that it's on the main thread to achieve what I need? Would it then call the BufferEnd event properly? I ask because making these changes will require a fair bit of redoing the code and mixing the audio layer into the main app which I would rather avoid.
Yeah, I don't know for sure because I work mostly from a console app, but if you are losing sound when when you change focus there must be some crosstalk between the windows messaging driving the application and XAudio2. You may be able to avoid this by pretending that your application is a console application and not a windows application (in other words not using Application.Run) and keeping your main thread alive. It may be enough to do the initialization of XAudio2 from your main thread (Create the object and start the engine) before you show your form.

Sorry this is mostly speculation. I am no expert on this (so don't take my suggestions as being super-authoritative or anything like that). I just hate see the XAudio2 questions slip off into the void when there is so little info available on the subject.

This topic is closed to new replies.

Advertisement