在控制台中使用NAudio播放MP3文件时,我不知道如何停止播放。当我调用waveout.Stop()时,代码只会停止运行,并且waveout.Dispose()不会被调用。
与函数回调有关吗?如果是,该如何解决?
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;
}
最佳答案
您是说代码卡在对waveOutReset的调用中吗?如果是这样,这是函数回调和某些音频驱动程序的已知问题(SoundMAX似乎特别容易受到此影响)。几个月前,我检查了NAudio源代码的可能修复,因此您可以尝试构建最新的代码,看看是否可以解决问题。