3

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

2 回答 2

3

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.

于 2010-05-12T06:47:59.970 回答
0

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

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

于 2010-05-11T15:33:49.640 回答