【问题标题】:Detecting if music has ended in VB.NET检测音乐是否在 VB.NET 中结束
【发布时间】:2015-03-30 20:38:04
【问题描述】:

标题说明了一切 - 我在 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。如何让它检测音乐文件何时播放完毕?

【问题讨论】:

  • 我应该补充一点,此代码作为我的媒体播放器的“播放”按钮的一部分存在
  • 音乐结束,关灯

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


【解决方案1】:

您可以使用 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

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2013-12-06
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多