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

此生再无相见时 提交于 2019-11-28 01:19:44

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:\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

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.

i looke into this and problem is that you trying too hard to make it so. YOu see, if you analyze the content of of the encapsulated MP3 player files, you will find that the sound barrier is converted to analog files instead of binary digital. This problem is when only allocated to the "WMP" as you say it video-audo play. If you switch opperating system it might work but i would go ahead and just buy a new computer first then use it instead. thanks and ty :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!