【高分】VB中的Windowsmediaplayer控件,如何实现自动播放下一个视频文件

2024年12月01日 09:36
有5个网友回答
网友(1):

你看看这个代码:
Dim b As Integer
Private Sub Command1_Click()
a = 0: b = 0
p = App.Path + "\data\"
If Dir(p & "*.avi") = "" Then MsgBox "没有avi文件": Exit Sub

Dim filenames() As String '读取目录下的avi文件
Dim f As String
n = 0
f = Dir(p & "*.avi")
Do While f <> ""
ReDim Preserve filenames(n)
filenames(n) = f
f = Dir
n = n + 1
Loop

Set sPlaylist = WindowsMediaPlayer1.playlistCollection.newPlaylist("avi") '建立新播放列表avi
WindowsMediaPlayer1.currentPlaylist = sPlaylist
For i = 0 To n - 1
Set sMedia = WindowsMediaPlayer1.mediaCollection.Add(p & filenames(i)) '将文件加入到播放列表中
WindowsMediaPlayer1.currentPlaylist.insertItem (WindowsMediaPlayer1.currentPlaylist.Count), sMedia '添加播放列表项
Next
WindowsMediaPlayer1.Controls.play '开始播放

End Sub

Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)
If NewState = 10 Then b = b + 1 '当播放状态为准备就绪的判断方法
If b = 2 Then MsgBox "播放结束"
End Sub

网友(2):

最简单有效的方法是用timer控件测试是否停止播放。

以下的方法都是针对外置播放列表的使用。如果想用wmp内置播放列表可以使用 mafan8 的方法。
这是timer的方法。只是简单,但是并不推荐。
Private Sub Timer1_Timer()
If wmp.playState = 1 Then
mIndex = mIndex + 1
If mIndex = 4 Then
mIndex = 1
MsgBox "播放完毕!"
Else
wmp.URL = mList(mIndex)
wmp.Controls.play
End If
End If
End Sub

以下方法比较推荐,使用PlayStateChange事件,手上没avi,以mp3为例。
Dim mList() As String
Dim mIndex As Long
Dim IsPlayNext As Boolean

Private Sub cmdPlay_Click()
wmp.URL = mList(mIndex)
End Sub

Private Sub cmdStop_Click()
wmp.Controls.stop
End Sub

Private Sub Form_Load()
Dim l As Long
ReDim mList(1 To 3)
For l = 1 To 3
mList(l) = App.Path & "\" & l & ".mp3"
Next
mIndex = 1
End Sub

Private Sub wmp_PlayStateChange(ByVal NewState As Long)
If NewState = 8 Then
mIndex = mIndex + 1
If mIndex = 4 Then
mIndex = 1
MsgBox "播放完毕!"
Else
IsPlayNext = True
End If
End If
If NewState = 1 And IsPlayNext Then
wmp.URL = mList(mIndex)
End If
If NewState = 3 Then IsPlayNext = False
End Sub

网友(3):

窗体上添加一个WindowsMediaPlayer1和一个Command1

Private Sub Command1_Click()

Dim listCollection As IWMPPlaylistCollection

Dim list As IWMPPlaylist

Dim wmpMedia As IWMPMedia

Dim name As String

Dim songs(2) As String

Dim i As Integer

name = "test"
songs(0) = App.Path & "\data\1.avi"
songs(1) = App.Path & "\data\2.avi"
songs(2) = App.Path & "\data\3.avi"
Set listCollection = WindowsMediaPlayer1.playlistCollection
Set list = listCollection.newPlaylist(name)
WindowsMediaPlayer1.currentPlaylist = list

For i = 0 To UBound(songs)
Set wmpMedia = WindowsMediaPlayer1.newMedia(songs(i))
WindowsMediaPlayer1.currentPlaylist.appendItem wmpMedia
Next

WindowsMediaPlayer1.Controls.play
End Sub

Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)

Select Case NewState

Case 8

If WindowsMediaPlayer1.currentMedia.name = "3" Then
MsgBox "播放完毕"
End If

End Select

End Sub

网友(4):

首先你要确定你用的是哪个版本的VB的哈,还有那个控件的版本,才好给你的哈

网友(5):

我也在解决这个问题,可以共同探讨下