在 C# 中使用 AxWindowsMediaPlayer 的 PlayStateChange 事件一个接一个地播放视频

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

我正在尝试创建一个视频播放器,它使用 AxWindowsMediaPlayer 一个接一个地播放一组视频。它目前播放给定集中的一个视频,但不播放其他视频。我正在尝试实现下面示例 link 中显示的方法,以创建由 PlayStateChange 属性、"Media Finished Playing." 的案例 8 触发的自动播放方法,但是,甚至没有一个视频播放这样。

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

如果需要任何进一步的解释,请告诉我,我很乐意详细说明。对于为什么这种方法不起作用的任何见解,我将不胜感激,我是 C# 的新手并且没有完全掌握委托的所有复杂性。我是否需要实现一个 for 循环来滚动视频数据库,这里称为 it.video?这是我的代码:

 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(); }
        }
      }

更新了使用 BeginInvoke 的代码,需要以某种方式实现 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(); 
                    }
                }
            }));
        }
    }

实现我的特定目的的最佳方法是使用播放列表方法。延迟代码似乎会导致问题。这是我的解决方案:

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(); }
         }
        }