问题描述
在我当前的项目中,我有一个自制的音频播放器,它通过我的 musictimer() 函数进行操作.下面是一个子命令,当有人点击图片时,它命令音频播放器转到下一首歌曲.这非常有效.
In my current project I have a self-made audioplayer which is operated trough my musictimer() function. Below is a sub which orders the audioplayer to go to the next song when someone has clicked on a picture. This works perfectly.
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
下面有一个子命令,当一首歌曲播放完毕时,它命令播放器播放下一首歌曲.这个子也有效,但只有当我在那里有 MessageBox.Show("blabla") 行时.否则它只是忽略音乐计时器(下一个").显然,一直有弹出消息很烦人,所以我希望它消失.有谁知道这是怎么回事?我一无所知.
Below there is a sub which orders the player to play the next song when a song is finished playing. This sub also works but only when I have the MessageBox.Show("blabla") line in there. Otherwise it simply ignores the musictimer("next"). Obviously its quite annoying to have popup messages the entire times so I want it gone. Does anyone know whats going on? Im clueless.
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
我非常凌乱的音乐定时器功能.
My very messy musictimer function.
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
推荐答案
PlayStateChanged 事件非常臭名昭著.这实际上只是为了更新显示状态的 UI 元素.在那个事件中与玩家做任何事情都是非常麻烦的.对 MessagBox 的调用会产生影响,因为它会产生一个消息循环,这对于 ActiveX 控件来说总是一个大问题.
The PlayStateChanged event is quite notorious. It was really meant to just update a UI element that shows the state. Doing anything with the player in that event is very troublesome. A call to MessagBox can have an affect because it pumps a message loop, always a big deal for ActiveX controls.
避免麻烦的最好方法是延迟你的代码,让它在事件被触发并且玩家回到静止状态之后运行.通过使用 Control.BeginInvoke() 方法优雅地完成.像这样:
The best way to stay out of trouble is by delaying your code, making it run after the event was fired and the player is back into a quiescent state. Elegantly done by using the Control.BeginInvoke() method. Like this:
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
这篇关于函数调用仅在包含 MessageBox.Show() 时有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!