Reputation: 801
I am developing a video player with WPF.(in VB)
I have already created a MediaElement ,ListBox, "Next" button,
then start playing through reading ListBox,
and use "Next" to skip to next audio/video.
In "MediaEnded" event, i just copy all code in "Next" button.
Now, problem is coming,
Assume the Listbox has four audios(.mp3), "test1.mp3", "test2.mp3", ......
now playing is "test1.mp3", i push "Next" button, then now playing is "test2.mp3".
However, when i just let "test1.mp3" play completed, my player will not play "test2.mp3",
it plays "test3.mp3" or others in random.
This situation like "MediaEnded" event was processed for many times.
Private Sub MediaElement1_MediaEnded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MediaElement1.MediaEnded
nextmedia()
End Sub
Private Sub nextmedia()
Try
'pi is play index, start from 1, 0 is non playing
If pi <> 0 Then
If pi = ListBox_temp.Items.Count Then
Dim filename As String = ListBox_temp.Items.Item(0).ToString
MediaElement1.Source = New Uri(filename)
pi = 1
Else
Dim filename As String = ListBox_temp.Items.Item(pi).ToString
MediaElement1.Source = New Uri(filename)
pi = pi + 1
End If
End If
Catch ex As Exception
End Try
Window1.Title = "Video Sampler - " + CStr(pi) + ". " + CStr(ListBox1.Items.Item(pi - 1))
End Sub
Who can help me....
Upvotes: 0
Views: 1093
Reputation: 69979
I haven't tested it, but try the following.
Try
pi = If(pi < ListBox_temp.Items.Count - 1, pi + 1, 0)
Dim filename As String = ListBox_temp.Items.Item(pi).ToString
MediaElement1.Source = New Uri(filename)
Catch ex As Exception
End Try
This increments pi
unless the last ListBoxItem
is selected, in which case it sets it to zero.
Upvotes: 0