2

在我目前的项目中,我有一个自制的音频播放器,它通过我的 musictimer() 函数进行操作。下面是一个子命令,当有人点击图片时,它会命令音频播放器转到下一首歌曲。这完美地工作。

Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
    If (ListBox1.Items.Count - 1 > songBeingPlayed) Then
        musictimer("next")
    Else
        musictimer("stop")
    End If
End Sub

下面有一个子命令,当一首歌曲播放完毕时,它命令播放器播放下一首歌曲。这个 sub 也有效,但只有当我在那里有 MessageBox.Show("blabla") 行时。否则它只会忽略音乐计时器(“下一个”)。显然,一直弹出消息很烦人,所以我希望它消失。有谁知道发生了什么事?我一无所知。

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
        musictimer("next")
        MessageBox.Show("blabla")
    End If
End Sub

我非常混乱的音乐定时器功能。

Function musictimer(ByVal action)
    If action Is "initial" Then
        TextBox1.Text = "0:00"
        Timer1.Stop()
        secondsCounter = 1
        doubledigitsecondCounter = 0
        minuteCounter = 0
    End If

    If action Is "reset" Then
        TextBox1.Text = "0:00"
        Timer1.Stop()
        secondsCounter = 1
        doubledigitsecondCounter = 0
        minuteCounter = 0
        Me.AxWindowsMediaPlayer1.URL = ""
        changePlayButton("play")
    End If

    If action Is "start" Then
        If (ListBox1.Items.Count > 0) Then
            Me.AxWindowsMediaPlayer1.URL = directoryPath + listboxpl(songBeingPlayed)
            AxWindowsMediaPlayer1.Ctlcontrols.play()
            Timer1.Start()
            changePlayButton("pause")
        End If
    End If

    If action Is "pause" Then
        Timer1.Stop()
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
        changePlayButton("play")
    End If

    If action Is "next" Then
        If (ListBox1.Items.Count - 1 > songBeingPlayed) Then
            songBeingPlayed += 1
            musictimer("reset")
            musictimer("start")
            changePlayButton("pause")
        Else
            musictimer("pause")
        End If
    End If

    If action Is "previous" Then
        If (songBeingPlayed > 0) Then
            songBeingPlayed -= 1
            musictimer("reset")
            musictimer("start")
        End If
    End If
End Function
4

1 回答 1

5

PlayStateChanged 事件非常臭名昭著。它实际上只是为了更新显示状态的 UI 元素。在那种情况下对玩家做任何事情都是非常麻烦的。对 MessagBox 的调用可能会产生影响,因为它会产生一个消息循环,这对于 ActiveX 控件来说总是很重要。

避免麻烦的最好方法是延迟你的代码,让它事件被触发并且玩家回到静止状态之后运行。使用 Control.BeginInvoke() 方法优雅地完成。像这样:

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    If e.newState = WMPLib.WMPPlayState.wmppsStopped Then
        Me.BeginInvoke(New Action(AddressOf NextSong))
    End If
End Sub

Private Sub NextSong()
    musictimer("next")
End Sub
于 2013-09-02T17:05:07.090 回答