This project has moved. For the latest updates, please go here.

WaveOut.PlaybackStopped not firing

Jan 3, 2011 at 8:48 PM

Hi,

Does anyone have an idea why the PlaybackStopped event is not firing?  I am executing the following code:

// Create input stream
WaveStream mp3Reader = new Mp3FileReader(item.FileName);
WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
WaveStream blockedAlignedStream = new BlockAlignReductionStream(pcmStream);
_activeStream = new WaveChannel32(blockedAlignedStream);
_activeStream.PadWithZeroes = false;

_player = new WaveOut();
_player.PlaybackStopped += new EventHandler(_player_PlaybackStopped);
_player.Init(_activeStream);
_player.Play();

It's been a few days I've been trying to figure this out.  Please help!

TIA,
Matt 

Coordinator
Jan 4, 2011 at 9:59 AM

PlaybackStopped will only fire if the Read method from your source stream returns 0. It is probably the BlockAlignReductionStream that is stopping this happening. It may be easier to detect once the position has gone past the length. Have a look at the NAudio demo project for an example

Mark

Jan 4, 2011 at 2:23 PM

I looked in the demo projects but couldn't really find an example.  Could you maybe point me to the right project?

I went and added a timer with a 1 second interval.  Every elapse, it checks the position of the stream and the length to determine if the position is >= the length.  I played a song, but when the song was over, my event never fired.  Upon looking at the values for position and length, I found that position was almost half of the length and was not increasing no matter how many seconds I waited.  Why is that happening?

#region Play(AudioItem)
public void Play(AudioItem item)
{
    // Create input stream
    WaveStream mp3Reader = new Mp3FileReader(item.FileName);
    WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
    WaveStream blockedAlignedStream = new BlockAlignReductionStream(pcmStream);
    _activeStream = new WaveChannel32(blockedAlignedStream);
    _activeStream.PadWithZeroes = false;

    // Set up timer
    _tmr = new Timer();
    _tmr.Interval = 1000;
    _tmr.Elapsed += new ElapsedEventHandler(_tmr_Elapsed);

    _player = new WaveOut();
    _player.Init(_activeStream);
    _player.Play();
    _tmr.Start();
}
#endregion

#region _tmr_Elapsed(object, ElapsedEventArgs)
private void _tmr_Elapsed(object sender, ElapsedEventArgs e)
{
    if (_activeStream.Position >= _activeStream.Length)
    {
        _tmr.Stop();
        OnPlaybackComplete();
    }   //if
}
#endregion

Coordinator
Jan 5, 2011 at 10:33 AM

if you work with the latest code, the Mp3FileReader is much more accurate at reporting length, and offers the additional benefit of not requiring you to perform the format conversion (Read returns PCM). This will hopefully be released in the not too distant future as NAudio 1.4

Mark