如何显示 vb.net 中播放列表中的 Media Player 正在播放哪个文件?

How to display which file is playing by Media Player from Playlist in vb.net ?

我正在研究 vb.net 使用 AxWindowsMediaPlayer 的视频播放器应用程序

我已经成功创建了一堆视频文件的播放列表,并且播放完美。

我的播放列表中有 2 个以上的视频。

我想在播放列表中播放下一个视频文件时显示该文件名的消息框。

我应该使用哪个事件?以及如何显示当前正在播放的文件?

我使用以下代码创建播放列表。

    Private Sub PlayVideos()

        Try


        AxWindowsMediaPlayer1.uiMode = "full"


        Dim Playlist As IWMPPlaylist = AxWindowsMediaPlayer1.playlistCollection.newPlaylist("Playlist1")

            Dim VideoFile1 As WMPLib.IWMPMedia3 =  AxWindowsMediaPlayer1.newMedia(Path1.Trim)
        Playlist.appendItem(VideoFile1)

            Dim VideoFile2 As WMPLib.IWMPMedia3 = AxWindowsMediaPlayer1.newMedia(Path2.Trim)
        Playlist.appendItem(VideoFile2)

  AxWindowsMediaPlayer1.currentPlaylist = Playlist


        Catch ex As Exception
        End Try

    End Sub

这段代码应该可以做到。将它添加到 PlayStateChange 事件中。我有一个空的 try..catch 语句的原因是因为当媒体播放器正在更改项目时,playstatechange 事件会触发几次,但在加载下一首曲目之前,currentmedia.name 属性 是空和 returns System.NullReferenceException。最终 currentMedia.name 属性 被设置为新项目,一切都很顺利。可能有更好的方法来做到这一点,但它对我有用。

With AxWindowsMediaPlayer1
    Static Dim lasttrack As String = ""
    Try
        If .playState = WMPLib.WMPPlayState.wmppsPlaying And .currentMedia.name <> lasttrack Then
            MessageBox.Show("Current playing track is " & .currentMedia.name)
                lasttrack = .currentMedia.name
            End If
        Catch
        End Try
    End With

处理 CurrentItemChange 事件。

设置currentPlaylist后添加如下代码属性:

AddHandler AxWindowsMediaPlayer1.CurrentItemChange,
        Sub(s As Object, cic As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent)
            Dim new_item As IWMPMedia = cic.pdispMedia

            MsgBox(new_item.sourceURL)
        End Sub

播放状态更改帮助检查媒体播放器的操作是否更改将此添加到 form1

 Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
        If (AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsPlaying) Then



            TextBox2.Text = "Opening..."
        End If
        With AxWindowsMediaPlayer1
            Static Dim lasttrack As String = ""
            Try
                If .playState = WMPLib.WMPPlayState.wmppsPlaying And .currentMedia.name <> lasttrack Then
                    TextBox1.Text = (.currentMedia.name)
                    lasttrack = .currentMedia.name




                End If
            Catch
            End Try
        End With