NAudio 播放无法成功停止

发布于 2024-09-01 07:41:32 字数 1297 浏览 3 评论 0原文

使用 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;
}

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

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-09-08 07:41:32

您是说代码挂在对 waveOutReset 的调用中吗?如果是这样,这是函数回调和某些音频驱动程序的已知问题(SoundMAX 似乎特别容易受到此影响)。几个月前我检查了 NAudio 源代码的可能修复,因此您可以尝试构建最新的代码并查看是否可以解决问题。

Are you saying the code hangs in the call to waveOutReset? If so, this is a known issue with function callbacks and certain audio drivers (SoundMAX seems particularly susceptible to this). 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.

那小子欠揍 2024-09-08 07:41:32

我的第一个猜测是,由于您没有 console.read() 或类似的调用,因此您的代码执行得如此之快,以至于您看不到最终的写入行。

如果 NAudio 实现了 IDisposable,那么我建议使用 using 语句让 .NET 为您处理这个问题。

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.

If NAudio implements IDisposable then i would recommend using a using statement to have .NET handle this for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文