Search code examples
c#videoaxwindowsmediaplayer

playing one video after another using AxWindowsMediaPlayer's PlayStateChange event in C#


I am trying to create a video player which plays a set of videos one after another using AxWindowsMediaPlayer. It currently plays one video in the given set but not the others. I am trying to implement the method shown in the example link below in order to create an auto-play method triggered by Case 8 of PlayStateChange property, "Media Finished Playing." However, not even one video plays this way.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd562460(v=vs.85).aspx

Please let me know if any further explanation is needed and I will gladly elaborate. I would greatly appreciate any insight as to why this method is not working, I am relatively new to C# and do not fully grasp all of the complexities of delegates. Do I need to implement a for loop to scroll through the database of videos, known as it.video here? Here is my code:

 public void playItem(Item it)
    {
        player.CreateControl();
        player.Enabled = true;
        player.enableContextMenu = false;
        player.uiMode = "none";
        player.Name = "player";
        player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
        player.URL = it.video;
    }

void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {

       if (p_onset)
        { player.Ctlcontrols.play(); }
       else if (e.newState == 8) // Media Finished
        {
            PlayNext();
        }
        else
        {
            player.Ctlcontrols.play();
            if (!Vars.playOne)
           { PlayNext(); }
        }
      }

Updated Code using BeginInvoke, need to somehow implement EndInvoke:

public void playItem(Item it)
    {
        WMPLib.IWMPMedia media;
        WMPLib.IWMPPlaylist playlist = player.playlistCollection.newPlaylist("myplaylist");
        for (int x = 0; x < _presented.count; x++)       
        {
            media = player.newMedia(_presented.getItem(x).video);
            playlist.appendItem(media);

        }
        player.currentPlaylist = playlist;
        player.PlayStateChange += player_PlayStateChange;
    }

    private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (e.newState == 8)
        {
            this.player.BeginInvoke(new Action(() =>
            {
               if (p_onset)
                { 
                    player.Ctlcontrols.play(); 
                }
                else
                {
                    if (!Vars.playOne)
                    {
                       //playQueue++;
                       //PlayNext(); 
                    }
                }
            }));
        }
    }

Solution

  • The best way to go about my specific purpose was to use a playlist method. Delaying the code seemed to cause issues. Here is my solution:

    public void playItem(ItemsPool it) 
        {
            player.CreateControl();
            player.Enabled = true;
            player.enableContextMenu = false;
            player.uiMode = "none";
            player.Name = "player";
            player.Anchor = AnchorStyles.Left |  AnchorStyles.Right | AnchorStyles.Bottom;
            WMPLib.IWMPMedia media; 
            WMPLib.IWMPPlaylist playlist = player.playlistCollection.newPlaylist("myplaylist"); 
            for (int x = 0; x < it.count; x++) 
             { 
                media = player.newMedia(it.getItem(x).video); 
             playlist.appendItem(media);  
            } 
    
            player.currentPlaylist = playlist;
    
            if (p_onset)
            { player.Ctlcontrols.play(); }
           else
            {
                if (!Vars.playOne)
              { PlayNext(); }
             }
            }