Private Sub Command1_Click()
Dim i As Double
i = Me.MediaPlayer1.Duration
MsgBox "文件总时间为:" + gettime(i)
i = Me.MediaPlayer1.CurrentPosition
MsgBox "文件播放位置:" + gettime(i)
End Sub
Private Function gettime(ByVal num As Double) As String
Dim i As Double
i = num
Dim m As Long
Dim s As Long
Dim h As Long
m = i \ 60
s = i - m * 60
h = m \ 60
m = m - h * 60
Dim mstr As String
mstr = CStr(h) + "小时" + Format(CStr(m), "#00") + "分" + Format(CStr(s), "#00") + "秒"
gettime = mstr
End Function
Public Type Mp3tag
Artist As String ’Artist 存储歌手信息
Album As String ’Album 存储唱片专辑信息
Title As String ’Title 存储标题信息
Year As String ’Year 存储年代信息
Comments As String ’Comments 存储备注信息
Genre As Integer ’Genre 存储音乐风格序列
End Type
然后,定义一个获取MP3信息的函数,它将返回这些信息,代码如下:
Public Function GetMp3Tag(FName As String) As Mp3tag
Dim Artist As String
Dim Album As String
Dim Title As String
Dim Year As String
Dim Comments As String
Dim Genre As Integer
If FName = "" Then Exit Function
If Dir(FName) = "" Then Exit Function
Dim FileNum As Integer
FileNum = FreeFile ’得到一个自由的文件号
Dim strInput As String
Open FName For Binary Access Read As FileNum ’以二进制形式打开文件
If LOF(FileNum) < 128 Then
Close FileNum
Exit Function
End If
If strInput <> "TAG" Then ’如果没有发现信息标识,就关闭文件
Close FileNum
GoTo Done:
End If
strInput = Space(30)
Get FileNum, , strInput
Title = Trim(strInput)
strInput = Space(30)
Get FileNum, , strInput
Artist = Trim(strInput)
strInput = Space(30)
Get FileNum, , strInput
Album = Trim(strInput)
strInput = Space(4)
Get FileNum, , strInput
Year = Trim(strInput)
strInput = Space(30)
Get FileNum, , strInput
Comments = Trim(strInput)
strInput = Space(1)
Get FileNum, , strInput
Genre = Asc(strInput)
Done:
GetMp3Tag.Title = Title
GetMp3Tag.Artist = Artist
GetMp3Tag.Album = Album
GetMp3Tag.Year = Year
GetMp3Tag.Year = Comments
If Genre < 0 Or Genre > 254 Then Genre = 12
GetMp3Tag.Genre = CInt(Genre)
把以上代码写到一个模块中(.Bas),然后在窗体上加入5个TextBox和1个ComboBox控件,其中5个TextBox控件分别用来显示一首Mp3文件的以下信息:歌手、年代、唱片、评论、标题,ComboBox控件用来显示歌曲的风格。再放一个Command控件,其标题为“显示信息”,在它的Click事件中加入以下代码:
Private sub Command1_click()
Dim mp3Tag as Mp3tag
mp3tag = GetMp3Tag ("c:\Song.mp3")
Text1.Text = mp3tag.Artist
Text2.Text = mp3tag.Album
Text3.Text = mp3tag.Title
Text4.Text = mp3tag.Year
Text5.Text = mp3tag.Comments
Combo1.ListIndex = mp3tag.Genre
End Sub
在窗体的加载过程中加入如下代码:
Private Sub Form_Load()
Dim i As Integer,GenreArray() As String
GenreArray = Split(sGenreMatrix, "|")
For i = LBound(GenreArray) To UBound(GenreArray)
Combo1.AddItem GenreArray(i)
Next i