检测音乐是否已在VB.NET中结束

时间:2015-01-31 12:38:03

标签: vb.net if-statement events while-loop background-music

标题说明了一切 - 我在VB.NET中使用Windows Media Player作为我制作的音乐播放器的基础。我已经获得以下代码来检测当前播放的.mp3文件是否已经结束:

            'Checks to see if the player is still playing music
            While WMPLib.WMPPlayState.wmppsPlaying
                    If WMPLib.WMPPlayState.wmppsMediaEnded Then
                    MessageBox.Show("Playing next song")
                    End If
            End While

while检查可以成功地看到正在播放音乐文件,但是当音乐文件结束时IF语句没有被检测到,它实际上在媒体当前正在播放时返回true。如何在音乐文件播放完毕后检测它?

1 个答案:

答案 0 :(得分:2)

您可以使用CurrentItemChange()和PlayStateChange()事件来查看播放器中发生的事情:

Private Sub AxWindowsMediaPlayer1_CurrentItemChange(sender As Object, e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles AxWindowsMediaPlayer1.CurrentItemChange
    Debug.Print("CurrentItemChange: " & Me.AxWindowsMediaPlayer1.currentMedia.name)
End Sub

Private Sub AxWindowsMediaPlayer1_PlayStateChange(sender As Object, e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    Select Case e.newState
        Case 1 ' Stopped
            Debug.Print("Stopped")

        Case 2    ' Paused
            Debug.Print("Paused")

        Case 3 ' Playing
            Debug.Print("Playing")

        Case 4 ' ScanForward
            Debug.Print("ScanForward")

        Case 5 ' ScanReverse
            Debug.Print("ScanReverse")

        Case 6 ' Buffering
            Debug.Print("Buffering")

        Case 7 ' Waiting
            Debug.Print("Waiting")

        Case 8 ' MediaEnded
            Debug.Print("MediaEnded")

        Case 9 ' Transitioning
            Debug.Print("Transitioning")

        Case 10 ' Ready
            Debug.Print("Ready")

        Case 11 ' Reconnecting
            Debug.Print("Reconnecting")

        Case 12 ' Last
            Debug.Print("Last")

        Case Else
            Debug.Print("Undefined/Unknown: " & e.newState)

    End Select
End Sub
相关问题