当 PlayStateChange 更改为 play 时,windows media player 停止播放歌曲 [英] windows media player stops of playing the song when PlayStateChange changed to play

查看:16
本文介绍了当 PlayStateChange 更改为 play 时,windows media player 停止播放歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    If AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsMediaEnded Then
        Dim str As Integer
        'the original lblPlayItemIndex.Text = 0 : the index of the first item in the listview
        str = lblPlayItemIndex.Text + 1
        AxWindowsMediaPlayer1.URL = fmPlaylist.lstPlaylist.Items(str).Tag
        AxWindowsMediaPlayer1.Ctlcontrols.play()
        MsgBox("str: " & str)
    End If
End Sub

我有一个列表视图作为媒体播放器的播放列表....listview中每一项的.tag就是歌曲的路径

I have a listview as a playlist for media player.... the .tag of each item in the listview is the path for the song

lblPlayItemIndex.Text"是listviewitem的索引,当歌曲结束时......它将使lblPlayItemIndex.Text"+1以获取listview中的下一个项目

the "lblPlayItemIndex.Text" is the index of the listviewitem and when the song end ... it will make the "lblPlayItemIndex.Text" +1 to get the next item in the listview

实际上我做得很好......但是当我关闭消息时,歌曲停止播放的问题......

actually I made it good....but the problem when I close the message the song stops of playing....

我的代码有什么问题吗....还是我应该用另一种方式来做??!!

is there anything wrong in my code .... or should I make it with another way??!!

推荐答案

当玩家状态改变时,它会改变很多次,你可以看到如果你将它们打印到控制台窗口.在尝试播放下一个之前,您需要留出时间让这些停止.这将展示 3 种方法.

When the player state changes, it changes many many times as you can see if you print them to the console window. You need to allow time for these to stop before you try to play the next one. This will show 3 ways to do that.

所有三种补救措施都需要一个方法 (Sub) 来播放下一首事件的歌曲:

All three remedies need a method (Sub) to play the next song which is not part of an event:

Private Sub PlayNextItem()

    ' however you figure out what to play next...example
    If chkRandom.Checked Then
        NextIndex = RNG.Next(0, SongList.Length)
    Else
        NextIndex = If(NextIndex + 1 > SongList.Count - 1, 0, NextIndex + 1)
    End If

    AxWMP.URL = SongList(NextIndex)
    lblNowPlaying.Text = SongList(NextIndex)

    AxWMP.Ctlcontrols.play()
End Sub

方法一

你经常看到这个,因为它很容易理解.从PlayStateChange 事件中,激活一个计时器;然后从 Timer_Tick 事件中,调用您的新方法.500 的间隔应该足以让它过渡:

You see this a lot because it is easily understood. From thePlayStateChange event, activate a Timer; then from the Timer_Tick event, call your new method. An interval of 500 should be enough time for it to transition:

游戏状态改变事件:

 If AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsMediaEnded Then
        Timer2.Enabled = True
 End If

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    PlayNextItem()
    ' be sure to turn off the timer
    Timer1.Enabled = False
End Sub

<小时>

方法 2 更简单 - 使用委托:


Method 2 is simpler - use a delegate:

Delegate Sub PlayNextDelegate()
Dim PlayNext As PlayNextDelegate = AddressOf PlayNextItem

然后从播放状态改变事件:

Then from the playstate changed event:

Me.BeginInvoke(PlayNext)

<小时>

也许更简单的是这个答案中的方法 3,其中包括对正在发生的事情的解释.


Perhaps simpler is Method 3 from this answer which includes an explanation of whats going on.

Me.BeginInvoke(New MethodInvoker(AddressOf PlayNextItem))

它仍然依赖一个单独的方法(Sub)来播放下一首歌曲,但您不需要声明和设置一个委托,即编织到调用中.

It still relies on a separate method (Sub) to play the next song, but you dont need to declare and setup a delegate, that is woven into the call.

这篇关于当 PlayStateChange 更改为 play 时,windows media player 停止播放歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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