windows media player stops of playing the song when PlayStateChange changed to play

我怕爱的太早我们不能终老 提交于 2019-12-11 21:19:29

问题


this is my code:

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

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

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??!!


回答1:


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.

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

Method 1

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:

Playstate change event:

 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

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)

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

Me.BeginInvoke(New MethodInvoker(AddressOf PlayNextItem))

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.



来源:https://stackoverflow.com/questions/19733620/windows-media-player-stops-of-playing-the-song-when-playstatechange-changed-to-p

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