无法在 VisualBasic 中调用 WMP 的 control.play() 函数 [英] Unable to call WMP's controls.play() function in VisualBasic

查看:21
本文介绍了无法在 VisualBasic 中调用 WMP 的 control.play() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:http://pastebin.com/EgjbzqA2 这基本上只是 http://www.dreamincode.net/forums/topic/57357-mymusic-player/.我想让程序重复播放一个文件,但是这个功能由于某种原因不起作用.程序播放每个文件一次然后停止.

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

推荐答案

PlayAllowed doo-wop 是一种黑客技术,可以解决当您要求控件在事件中执行其他操作时变得暴躁的控件.这通常会出错,他们不希望在触发事件时地板垫会猛地一跳.技术术语是它们不能很好地处理重入,这是一个非常普遍的问题.

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.

对于重入问题有一个非常优雅的解决方案,关键是您延迟再次播放同一首歌曲的请求,在事件引发之后.在 Winforms 中,您可以通过使用 Control.BeginInvoke() 轻松获得这样的延迟,目标在一切安定下来后运行.技术术语是等待程序重新进入消息循环".在这段代码上效果很好,我用这段代码一遍又一遍地循环播放同一首歌没有问题,在 Windows 8 上测试过:

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

根据需要调整代码,因为它不会很好地匹配您的代码.关键部分是 PlayStateChanged 事件处理程序中的 Me.BeginInvoke() 调用.

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.

这篇关于无法在 VisualBasic 中调用 WMP 的 control.play() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆