NAudio playback wont stop successfully

Posted by Kurru on Stack Overflow See other posts from Stack Overflow or by Kurru
Published on 2010-05-11T15:15:41Z Indexed on 2010/05/12 6:54 UTC
Read the original article Hit count: 447

Filed under:
|
|
|

Hi

When using NAudio to playback an mp3 [in the console], I cant figure out how to stop the playback. When I call waveout.Stop() the code just stops running and waveout.Dispose() never gets called.

Is it something to do with the function callback? I dont know how to fix that if it is.

        static string MP3 = @"song.mp3";
        static WaveOut waveout;
        static WaveStream playback;
        static void Main(string[] args)
        {
            waveout = new WaveOut(WaveCallbackInfo.FunctionCallback());
            playback = OpenMp3Stream(MP3);
            waveout.Init(playback);
            waveout.Play();
            Console.WriteLine("Started");

            Thread.Sleep(2 * 1000);

            Console.WriteLine("Ending");
            if (waveout.PlaybackState != PlaybackState.Stopped)
                waveout.Stop();
            Console.WriteLine("Stopped");
            waveout.Dispose();
            Console.WriteLine("1st dispose");
            playback.Dispose();
            Console.WriteLine("2nd dispose");
        }
        private static WaveChannel32 OpenMp3Stream(string fileName)
        {
            WaveChannel32 inputStream;
            WaveStream mp3Reader = new Mp3FileReader(fileName);
            WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
            WaveStream blockAlignedStream = new BlockAlignReductionStream(pcmStream);
            inputStream = new WaveChannel32(blockAlignedStream);
            return inputStream;
        }

© Stack Overflow or respective owner

Related posts about naudio

Related posts about c#