我有以下代码:
http://pastebin.com/EgjbzqA2基本上只是
http://www.dreamincode.net/forums/topic/57357-mymusic-player/的精简版本.我希望程序重复播放一个文件,但是,此功能由于某种原因不起作用.程序播放每个文件一次然后停止.
Private Sub Player3_PlayStateChange(ByVal NewState As Integer) Handles Player3.PlayStateChange Static Dim PlayAllowed As Boolean = True Select Case CType(NewState,WMPLib.WMPPlayState) Case WMPLib.WMPPlayState.wmppsReady If PlayAllowed Then Player3.controls.play() End If Case WMPLib.WMPPlayState.wmppsMediaEnded ' Start protection (without it next wouldn't play PlayAllowed = False ' Play track Player3.controls.play() ' End Protection PlayAllowed = True updatePlayer() End Select End Sub
当你要求它在事件中做其他事情时,PlayAllowed doo-wop是hackorama来解决控制变得暴躁的问题.这往往是错误的,他们不希望地板垫在发生事件时被猛拉.技术术语是它们不能很好地处理重入,这是一个非常普遍的问题.
对于重新入侵问题有一个非常优雅的解决方案,关键是在事件发生后你再次延迟播放同一首歌的请求.在Winforms中,您可以通过使用Control.BeginInvoke()轻松获得此类延迟,目标在所有内容恢复后运行.技术术语是“等待程序重新进入消息循环”.在这段代码上运行得非常好,我在使用此代码时反复循环播放同一首歌,并在Windows 8上进行了测试:
Public Class Form1 Dim WithEvents Player3 As New WMPLib.WindowsMediaPlayer Dim Song As String = "c:\temp\ding.wav" Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click PlayCurrentSong() End Sub Private Sub Player3_PlayStateChange(ByVal NewState As Integer) Handles Player3.PlayStateChange If NewState = WMPLib.WMPPlayState.wmppsMediaEnded Then Me.BeginInvoke(New MethodInvoker(AddressOf PlayCurrentSong)) End If End Sub Private Sub PlayCurrentSong() Player3.URL = Song Player3.controls.play() End Sub End Class
根据需要调整代码,因为它与您的代码不相符.关键部分是PlayStateChanged事件处理程序中的Me.BeginInvoke()调用.