Audio playback will not stop
When using NAudio to play MP3 [in the console], I can't figure out how to stop playing. When I call waveout.Stop () the code just stops working and waveout.Dispose () is never called.
How does this relate to the function callback? If so, how to fix it?
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;
}
+2
a source to share
2 answers
Are you saying that the code hangs in the waveOutReset call? If so, this is a known issue with function callbacks and some sound drivers (SoundMAX seems especially susceptible to this). I checked the NAudio source code on the patch a couple of months ago, so you can try building the latest code and see if that fixes the problem.
+2
a source to share