在 WPF 中使用 MediaElement 循环播放音频
我正在用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有测试过,但请尝试以下操作。
除非选择了最后一个
ListBoxItem
,否则它会递增pi
,在这种情况下,会将其设置为零。I haven't tested it, but try the following.
This increments
pi
unless the lastListBoxItem
is selected, in which case it sets it to zero.