Search code examples
c#playlistaxwindowsmediaplayer

How to detect the end of an AxWindowsMediaPlayer playlist


I currently have a program which plays a set of videos from a playlist then displays a set of images once the videos are done playing. I am currently using the PlayStateChange event to count each time a video is ending but this results in the images displaying at the beginning of the last video and not at the end. I need a way to trigger the images when the playlist is completely done playing. I even tried delaying the code but that causes my display image method to act screwy. Here is a code sample, not that it is really needed to answer my question.

    private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (!currSess.playOne)
        {
            Console.WriteLine(player.playState);
            if (e.newState == 8 | e.newState == 9)
            {
                if (e.newState == 8)
                {
                    currSess.playQueue++;
                    Console.WriteLine(currSess.playQueue);
                }

            }

                if (currSess.playQueue+1 > player.currentPlaylist.count -1)
                {
                //    await Task.Delay(1000);
                    displayImgs(); 
                    player.PlayStateChange -= foo;
                    currSess.playQueue = 0;
                }
        }
    }

Solution

  • Fixed by adding another if statement, since if the last video is done playing it will assume the ready state and stop there:

       if (currSess.playQueue+1 > player.currentPlaylist.count -1)
                    {
                        if (e.newState == 10)
                        {
                            //    await Task.Delay(1000);
                            displayImgs();
                            currSess._timer.start();
                            Console.WriteLine(currSess._timer);
                            AllowControls(true);
                            allowItems();
                            player.PlayStateChange -= foo;
                            currSess.playQueue = 0;
                        }
                    }