1

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(); 
                    }
                }
            }));
        }
    }
Harrison
  • 57
  • 11
  • There is very little you can do inside your PlayStateChange event handler. It was meant to update the state or your own UI, WMP is not re-entrant when this is event is fired. It is busy changing state, you cannot do anything that changes state. Lots of duplicates, [one](http://stackoverflow.com/questions/18577063/function-call-only-works-when-messagebox-show-is-included/18577936#18577936), [two](http://stackoverflow.com/a/13792759/17034). – Hans Passant Sep 15 '16 at 21:42
  • @HansPassant If using the BeginInvoke method, how would I called EndInvoke? I implemented this method but the code freezes after all the videos play. Thus I think EndInvoke is needed to end the asynchronously runnning code but I cannot figure out how to implement it properly – Harrison Sep 23 '16 at 19:17

1 Answers1

0

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(); }
         }
        }
Harrison
  • 57
  • 11