3

我对使用 NAudio 有点陌生,所以我肯定错过了一些重要的东西,因此这个问题。

从示例/演示代码开始,我整理了一个尽可能简单的类来播放 MP3 或 WAV 文件:

public class AudioPlayer : IDisposable
{
    WaveStream _waveStream; // sound data stream
    WaveChannel32 _waveChannel32; // sound channel ??
    IWavePlayer _iWavePlayer; // sound output driver

    public bool IsPlaying
    {
        get { return _iWavePlayer != null 
                     && _iWavePlayer.PlaybackState 
                         != Wave.PlaybackState.Stopped; }
    }

    public PlaybackState PlaybackState
    {
        get { return _iWavePlayer.PlaybackState; }
    }

    public void LoadMp3(byte[] mp3Bytes)
    {
        Load(CreateInputStreamFromMp3Bytes(mp3Bytes));
    }

    public void LoadFile(string filename)
    {
        Load(CreateInputStream(filename));
    }

    void Load(WaveStream waveStream)
    {
        if (_iWavePlayer != null)
            Dispose();
        _iWavePlayer = new WaveOut();
        _iWavePlayer.PlaybackStopped += 
            new EventHandler(_iWavePlayer_PlaybackStopped);
        _waveStream = waveStream;
        _waveChannel32 = new WaveChannel32(_waveStream);
        _waveChannel32.PadWithZeroes = false;
        _iWavePlayer.Init(_waveChannel32);
    }

    void _iWavePlayer_PlaybackStopped(object sender, EventArgs e)
    {
        Stop();
    }

    static WaveStream CreateInputStreamFromMp3Bytes(byte[] mp3Bytes)
    {
        return new Mp3FileReader(new MemoryStream(mp3Bytes), false);
    }

    static WaveStream CreateInputStream(string fileName)
    {
        if (fileName.EndsWith(".wav"))
            return new WaveFileReader(fileName);
        else if (fileName.EndsWith(".mp3"))
            return new Mp3FileReader(fileName);
        else
            throw new InvalidOperationException("Unsupported extension");
    }

    public void Play()
    {
        _iWavePlayer.Play();
    }

    public void Stop()
    {
        if (_iWavePlayer != null 
                && _iWavePlayer.PlaybackState != PlaybackState.Stopped) { 
            _iWavePlayer.Stop();
            _waveStream.Position = 0;
        }
    }

    public void Dispose()
    {
        Stop();
        if (_iWavePlayer != null) {
            _iWavePlayer.Dispose();
            _iWavePlayer = null;
        }
        if (_waveChannel32 != null) {
            _waveChannel32.Dispose();
            _waveChannel32 = null;
        }
        if (_waveStream != null) {
            _waveStream.Dispose();
            _waveStream = null;
        }
    }
}

我正在使用有问题的代码播放 MP3 文件(不是 WAV)。

它在大多数情况下都可以正常工作,考虑到我将能够在我的项目中替换 MediaElements,这非常棒。我遇到的问题是,一旦 PlaybackStopped 发生火灾,就会PlaybackState停留在Playing而不是改变。Stopped为了解决这个问题,我订阅并从那里PlaybackStopped打电话。Stop()

我必须这样打电话是正常的Stop(),还是我在这里遗漏了什么?

4

2 回答 2

2

看起来像一个错误。如果您查看 NAudio 代码将WaveOut不会更改PlaybackState. RaisePlaybackStoppedEventDirectSoundOut将其更改为停止。有点疯狂。

于 2012-08-09T14:24:08.200 回答
1

尝试这个:

在 Load 方法中更改此行:

_iWavePlayer = new WaveOut(); 

这样:

_iWavePlayer = new WaveOut(WaveCallbackInfo.FunctionCallback());
于 2014-05-29T14:01:25.820 回答