使用 NAudio 播放 MP3 - Stop() 问题

发布于 2024-12-03 10:24:51 字数 1594 浏览 9 评论 0原文

我刚刚开始仅使用 NAudio (1.4) 来播放 MP3。我一直在研究示例的文档和源代码。目前我在课堂上有这个:

    IWavePlayer waveOutDevice;
    WaveStream mainOutputStream;
    WaveChannel32 volumeStream;

    public AudioPlaybackService() : base() {
        waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);    
    }

    public bool LoadTrack(string trackPath, float volume)
    {
        if (!File.Exists(trackPath))
            return false;
        try
        {
            mainOutputStream = new Mp3FileReader(trackPath);
            volumeStream = new WaveChannel32(mainOutputStream);
            volumeStream.Volume = volume;
            waveOutDevice.Init(mainOutputStream);
        }
        catch (Exception e)
        {
            Logger.Error("Failed to load track for playback {0} :: {1}", trackPath, e.ToString());
            return false;
        }
        return true;
    }

    public bool PlayTrack()
    {
        if (waveOutDevice == null || waveOutDevice.PlaybackState == PlaybackState.Playing)
            return false;
        waveOutDevice.Play();
        return true;
    }

    public bool StopTrack()
    {
        if (waveOutDevice == null || waveOutDevice.PlaybackState == PlaybackState.Stopped)
            return false;
        waveOutDevice.Stop();
        mainOutputStream.CurrentTime = TimeSpan.Zero;
        return true;
    }

这可以很好地加载并播放我的测试曲目,我的问题是 Stop() 函数。首先,我应该在调用 Stop() 后重置 CurrentTime 属性吗?目前,它的作用更像是一个暂停按钮,即它在“停止”的同一位置恢复曲目。如果我确实需要重置当前时间,我现在会遇到一个问题,如果我单击“停止”,曲目就会停止,但如果我之后再次单击“播放”,我会在曲目再次开始之前收到一些剩余的噪音。

查看其中一个示例的源代码,它所做的只是调用 Stop()。

I've just started using NAudio (1.4) solely for MP3 playback. I've been working off the documentation and the source code for the samples. Currently I have this in a class:

    IWavePlayer waveOutDevice;
    WaveStream mainOutputStream;
    WaveChannel32 volumeStream;

    public AudioPlaybackService() : base() {
        waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);    
    }

    public bool LoadTrack(string trackPath, float volume)
    {
        if (!File.Exists(trackPath))
            return false;
        try
        {
            mainOutputStream = new Mp3FileReader(trackPath);
            volumeStream = new WaveChannel32(mainOutputStream);
            volumeStream.Volume = volume;
            waveOutDevice.Init(mainOutputStream);
        }
        catch (Exception e)
        {
            Logger.Error("Failed to load track for playback {0} :: {1}", trackPath, e.ToString());
            return false;
        }
        return true;
    }

    public bool PlayTrack()
    {
        if (waveOutDevice == null || waveOutDevice.PlaybackState == PlaybackState.Playing)
            return false;
        waveOutDevice.Play();
        return true;
    }

    public bool StopTrack()
    {
        if (waveOutDevice == null || waveOutDevice.PlaybackState == PlaybackState.Stopped)
            return false;
        waveOutDevice.Stop();
        mainOutputStream.CurrentTime = TimeSpan.Zero;
        return true;
    }

This loads and plays my test track fine, my issue is with the Stop() function. Firstly should I need to reset the CurrentTime property after calling Stop()? Currently it acts more like a pause button i.e. it resumes the track in the same place it was "stopped". If I do need to reset the CurrentTime I now have a problem where if I click stop, the track stops, but if I click play again afterwards I get a little leftover noise before the track starts again.

Looking at the source code of one of the samples all it does is call Stop().

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

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

发布评论

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

评论(1

人生戏 2024-12-10 10:24:51

我们对naudio的使用中,我们从未停止过音频。任何类似停止的功能都会导致无声波形(零)被馈送到波输出。这主要是由于过于频繁地停止和启动时导致 naudio 不稳定,但它也防止了“剩余缓冲区”问题。

In our use of naudio, we never stop the audio. Any stop-like functionality causes a silent waveform (zeroes) to be fed to the wave out. This was mainly due to instability in naudio when stopping and starting too frequently, but it also prevents the "leftover buffer" problem.

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