2012-12-07 36 views
5

我有以下代碼:http://pastebin.com/EgjbzqA2這基本上只是一個精簡版本http://www.dreamincode.net/forums/topic/57357-mymusic-player/。我希望程序反覆播放一個文件,但是,此功能因某種原因不起作用。該程序播放每個文件一次,然後停止。無法在VisualBasic中調用WMP的controls.play()函數

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 

回答

1

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()調用。

+1

工程就像一個魅力!非常感激。 – ahota

+0

lol^tenmorechar – Dylansq

0

我喜歡這個,問題在於你努力的努力去做到這一點。你看,如果你分析封裝的MP3播放器文件的內容,你會發現聲音障礙被轉換成模擬文件而不是二進制數字。這個問題是當你說視頻 - 音頻播放時只分配給「WMP」。如果您切換操作系統,它可能會工作,但我會繼續前進,只是先購買一臺新電腦,然後使用它。 感謝和ty :)