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.