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

How can i re-initialize an audio stream after it's been interrupted by a WASAPI exclusive mode application?

May 24, 2012 at 1:15 AM

I am writing a program which plays a wav file in WASAPI shared mode, using the following code:

Dim wavplayer As NAudio.Wave.IWavePlayer
            Dim wavfile As NAudio.Wave.WaveStream = New NAudio.Wave.WaveFileReader(AVRF.My.Resources.silence)
            Dim wavStream = New NAudio.Wave.WaveChannel32(wavfile)
            wavplayer = New NAudio.Wave.WasapiOut(AudioClientShareMode.Shared, 100)
            wavplayer.Init(wavStream)
            wavplayer.Play()

this works until a WASAPI exclusive mode application takes over, which causes playback of the WAV to stop, although the playbackstate stays on playing. The stopping of the WAV is actually what i want to happen so i'm happy enough with this.

However, If i try to use wavplayer.init, wavplayer.stop or wavplayer.play while the WASAPI exclusive stream is already playing, the program throws back a com exception, sometimes even when there's a handler in place to stop it.

What is the best way to keep retrying to play the wav file without causing an exception? i want it to start playing again as soon as the WASAPI exclusive stream ends

thankyou

Marc

May 24, 2012 at 12:45 PM

have figured this out myself now  :)

Coordinator
May 25, 2012 at 9:01 AM

glad you got it working. did you need to make any modifications to WasapiOut?

May 25, 2012 at 9:05 AM
Edited May 25, 2012 at 9:07 AM

You'll probably cringe, but i basically have a timer which looks at the stream.position and if the stream stops moving, it takes that to mean that an exclusive-mode stream has kicked in, so i dispose and recreate the wavplayer object and if the init command fails i try again on a timer until it doesn't fail, hence the wasapi exclusive-mode stream has ended.

Now that i managed to get my error handling working properly, the program continues running even though technically it's erroring constantly, and it doesn't use up a lot of resources even with the timer

if you know of a better way, using ducking for instance, i would gladly change it :)

Coordinator
May 25, 2012 at 9:09 AM

OK, sounds hacky but if it works, who cares right? :)

I was wondering if WASAPI has the ability to report to you somehow that exclusive mode has been activated or deactivated.

May 25, 2012 at 9:14 AM
Edited May 25, 2012 at 9:42 AM

i think "hacky" is the perfect way to describe a lot of my programming :D

there is functionality in the Core Audio API for stream notifications but for the life of me i couldn't work out how to use coreaudio.dll, and it seems i'm not alone in that regard judging by how little it's documented, and the msdn whitepapers are way over my head.

There's also meant to be a way to set your stream to use "ducking" which means you can tell it to pause while another stream is active and resume afterwards (or its original intended use, to lower the volume of your sound when you get a skype call for instance), but i don't know if that's implemented in NAudio, or if it applies to exclusive-mode stream, but if it is in there somewhere, and does work with exclusive-modes streams it would probably solve all my problems