简体   繁体   English

NAudio播放不会成功停止

[英]NAudio playback won't stop successfully

When using NAudio to playback an MP3 file [in the console], I can't figure out how to stop the playback. 当使用NAudio播放MP3文件[在控制台]时,我无法弄清楚如何停止播放。 When I call waveout.Stop() the code just stops running and waveout.Dispose() never gets called. 当我调用waveout.Stop()时,代码停止运行并且waveout.Dispose()永远不会被调用。

Has it something to do with the function callback? 它与函数回调有关吗? If it is, how do I 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;
}

Are you saying the code hangs in the call to waveOutReset? 你是说代码在对waveOutReset的调用中挂起? If so, this is a known issue with function callbacks and certain audio drivers (SoundMAX seems particularly susceptible to this). 如果是这样,这是函数回调和某些音频驱动程序的已知问题(SoundMAX似乎特别容易受此影响)。 I checked in a possible fix to the NAudio source code a couple of months ago, so you could try building the latest code and seeing if that fixes the issue. 我几个月前检查了一个可能的修复NAudio源代码,所以你可以尝试构建最新的代码,看看是否能解决问题。

My first guess here is that since you have no console.read() or similar call that your code is just executing so fast that you don't see the final write lines. 我在这里的第一个猜测是,因为你没有console.read()或类似的调用,你的代码执行速度太快,你没有看到最后的写行。

If NAudio implements IDisposable then i would recommend using a using statement to have .NET handle this for you. 如果NAudio实现了IDisposable,那么我建议使用using语句让.NET为你处理这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM