在 WPF 中使用 MediaElement 循环播放音频

发布于 2024-12-09 04:55:47 字数 1373 浏览 1 评论 0原文

我正在用WPF开发一个视频播放器。(在VB中)

我已经创建了一个MediaElement,ListBox,“下一步”按钮,

然后通过读取ListBox开始播放,

并使用“下一步”跳到下一个音频/视频。

在“MediaEnded”事件中,我只需复制“下一步”按钮中的所有代码。


现在,问题来了,

假设列表框有四个音频(.mp3),“test1.mp3”,“test2.mp3”,……

现在播放的是“test1.mp3”,我按“下一步”按钮,那么现在播放的是“test2.mp3”。

但是,当我让“test1.mp3”播放完成时,我的播放器将不会播放“test2.mp3”,

而是随机播放“test3.mp3”或其他内容。

像“MediaEnded”事件这样的情况已处理多次。


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

谁能帮我....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-12-16 04:55:47

我还没有测试过,但请尝试以下操作。

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

除非选择了最后一个 ListBoxItem,否则它会递增 pi,在这种情况下,会将其设置为零。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文