开发者

Circulate playing audio with MediaElement in WPF

开发者 https://www.devze.com 2023-04-12 07:30 出处:网络
I am developing a video player with WPF.(in VB) I have already created a MediaElement ,ListBox, \"Next\" button,

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....


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号