Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

vb.net - Unable to call WMP's controls.play() function in VisualBasic

I have the following code: http://pastebin.com/EgjbzqA2 which is basically just a stripped down version of http://www.dreamincode.net/forums/topic/57357-mymusic-player/. I want the program to play one file repeatedly, however, this function doesn't work for some reason. The program plays each file once and then stops.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The PlayAllowed doo-wop is hackorama to work around a control getting cranky when you ask it to do something else in an event. That often goes wrong, they don't expect the floor mat to be jerked when they fire an event. The technical term is that they don't handle re-entrancy well, a very common problem.

There's a very elegant solution to re-entrancy problems, the key is that you delay the request to play the same song again, after the event was raised. In Winforms you can easily get such a delay by using Control.BeginInvoke(), the target runs after everything settles back down. The technical term for that is "waiting for the program to re-enter the message loop". That worked very well on this code, I had no trouble looping the same song over and over again with this code, tested on Windows 8:

Public Class Form1
    Dim WithEvents Player3 As New WMPLib.WindowsMediaPlayer
    Dim Song As String = "c:empding.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

Tweak the code as needed since it won't match yours very well. The essential part is the Me.BeginInvoke() call in the PlayStateChanged event handler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...