NAudio 1.7 wasapi exclusive MediaFoundationReader

Aug 31, 2013 at 8:23 PM
I am trying to use wasapi exclusive mode in a WCF Service. I am having problems with next audio file to play in exclusive mode.

Shared mode:
Plays first source and then happily plays next source

Exclusive mode:
Plays the first source and then will not continue with the next source. I must IIS Reset to make it work again.

Also for some reason in a web service any call to Waveplayer.Dispose() causes an exception

Any thoughts on how I can get the next audio source to play?

public static IWavePlayer Waveplayer;
public static WaveStream FileWaveStream;

//Audio function
public static void PlayFile(string fileNameToPlay, Int32 playIndex)
//File handling/error checking code removed for brevity
 Waveplayer = new WasapiOut(dev, wasapisSareMode, wasapiEventCallbacks, wasapiLatency);
 Waveplayer.PlaybackStopped += WaveplayerPlaybackStopped;

FileWaveStream = new MediaFoundationReader(fileNameToPlay,
                                 new MediaFoundationReader.
                                         RepositionInRead = true,
                                         RequestFloatOutput = false,
                                         SingleReaderObject = false

     if (FileWaveStream == null) break;
     if (FileWaveStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
                 FileWaveStream = WaveFormatConversionStream.CreatePcmStream(FileWaveStream);
                  FileWaveStream = new BlockAlignReductionStream(FileWaveStream);
            Data.ContinuePlayingTracks = true;

private static void WaveplayerPlaybackStopped(object sender, StoppedEventArgs e)
            if (!Data.ContinuePlayingTracks) return;

            PlayFile(Data.LstMedia[Data.PlayIndex].FullPath + @"\" + Data.LstMedia[Data.PlayIndex].FileName, Data.PlayIndex);
Sep 3, 2013 at 11:02 AM
You need to dispose your WasapiOut before creating a new one. The original one is holding on to exclusive access.
Sep 5, 2013 at 1:31 AM
I have added in a CloseWaveOut() function. This works in exclusive mode via IIS on 2 of 3 computers I have tried it on. It does not however work with my M2Tech EVO usb to spdif convertor. In fact the only way to keep the EVO playing is to not call CloseWaveOut at all using shared mode. Any call to CloseWaveOut and the EVO will not play again until I IIS reset. This looks to be a driver issue with the EVO. Very sad as I wrote this program specifically for my main stereo using the EVO.
 private static void WaveplayerPlaybackStopped(object sender, StoppedEventArgs e)
            if (!Data.ContinuePlayingTracks) return;

            PlayFile(Data.LstServerMedia[Data.PlayIndex].FullPath + @"\" + Data.LstServerMedia[Data.PlayIndex].FileName, Data.PlayIndex);
public static void CloseWaveOut()
            if (Waveplayer != null)
                Thread.Sleep(200);  // Let the _waveplayer threads finish and exit

            if (FileWaveStream != null)
                // this one really closes the file and ACM conversion

            if (Waveplayer == null) return;

            Waveplayer = null;