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

How to know whether the playback has ended or no?

Sep 27, 2012 at 11:06 AM

Okay, I has a problem about getting wavestream status, whether it's position(current position in wavestream) and the total duration time. Any help would thanks!

Sep 28, 2012 at 6:30 PM

There are a few different ways to return the position of your audio file. How are you using WaveStream? Post your code and I'll be happy to help point you in the right direction

Sep 29, 2012 at 12:37 AM

This is the code snippet:

if (/*What should I put inside*/) /*In this condition, I want to test whether the mp3 files reach to the                
        end or not. But I don't know how to perform it*/
{
    if (localI < localFileNames.Length) //If yes
        {
            Clear();
            localI++; //Play next track
            CreateInputStream(localFileNames, localI);
            MP3Play();  
                                   
        }
    else //If no
        {
            Clear();
            localI = 0; // Replay the track list
            CreateInputStream(localFileNames, localI);
            MP3Play();
        }
}

IWavePlayer playerDevice;
WaveStream mainOutputStream; 
WaveChannel32 volumeStream;
string[] localFileNames = null;
int localI = 0;             

    public static WaveStream CreateInputStream(string[] fileNames, int i)
        {
            Array.Resize(ref localFileNames, fileNames.Length);
            localI = i;

             for (int i = 0; i < fileNames.Length; i++)
                {
                    localFileNames[i] = fileNames[i];
                }
          
            playerDevice = new WaveOut();
            WaveChannel32 inputStream;

            if (localFileNames[localI].EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(localFileNames[localI]);
                inputStream = new WaveChannel32(mp3Reader);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            volumeStream = inputStream;

            mainOutputStream = volumeStream;

            return mainOutputStream;
        }

    public static void MP3Play()
        {            
            playerDevice.Init(mainOutputStream);
            playerDevice.Play();            
        }
    
    public static void Clear()
        {
            playerDevice.Stop();

            volumeStream.Close();
            volumeStream = null;

            mainOutputStream.Close();
            mainOutputStream = null;

            playerDevice.Dispose();
            playerDevice = null;
        }

Coordinator
Sep 29, 2012 at 10:04 PM

subscribe to the PlaybackStopped event on playerDevice to detect when a file has ended. You will need to be using a relatively recent version of NAudio

 

Mark

Sep 30, 2012 at 2:39 AM

Thanks!

Sep 30, 2012 at 3:44 AM

For additional, i found that send the Mp3Reader to IWaveProvider instead of WaveStream can know whether the file has ended, plus using a timer. :)