VB WindowsMediaPlayer 播放

2024-11-14 18:34:44
推荐回答(1个)
回答(1):

‘添加windowsmediaplayer控件 一个timer控件即可使用

Option Explicit
Dim list2() As String, B可播放文件数 As Integer '可以播放的文件
Dim list() As String '存放所有的文件名
Dim total As Long '文件总个数
Dim now As Long '用来记录当前读取的文件的排序

Private Sub Form_Load()
Timer1.Interval = 200
now = 1
total = 0
getAll "J:\娱乐\李贞贤" '指定音乐文件夹 '输出文件的个数

Call F存入可播放文件名
If B可播放文件数 > 0 Then
Me.WindowsMediaPlayer1.URL = list2(now)
Else
MsgBox "没有音乐文件"
End If
End Sub

'遍历指定目录下的文件 并将所有的文件名放入数组list
Function getAll(rootF)
Dim fso As Object, folder As Object, subfolder As Object, file As Object
Set fso = CreateObject("scripting.filesystemobject") '创建FSO对象
Set folder = fso.getfolder(rootF) '得到文件夹对象
For Each subfolder In folder.subfolders '遍历子文件夹
Call getAll(subfolder) '递归,查找该文件夹的子文件夹
Next
For Each file In folder.Files '遍历根文件夹下的文件
Debug.Print folder
Debug.Print file '输出文件名

total = total + 1
ReDim Preserve list(total) As String
list(total) = file

Next
Set fso = Nothing
Set folder = Nothing
Set fso = Nothing
End Function

Function F存入可播放文件名()
Dim i As Integer
B可播放文件数 = 0
For i = 1 To total
If Right(list(i), 3) = "mp3" Or Right(list(i), 3) = "wav" Then
B可播放文件数 = B可播放文件数 + 1
ReDim Preserve list2(B可播放文件数)
list2(B可播放文件数) = list(i)
End If
Next
End Function

Private Sub Timer1_Timer()
If Me.WindowsMediaPlayer1.playState = wmppsStopped Then
now = now + 1
If now > B可播放文件数 Then now = 1
Me.WindowsMediaPlayer1.URL = list2(now)
Me.WindowsMediaPlayer1.Controls.play
End If
End Sub