No external controls make multimedia player (ii)

Source: Internet
Author: User
Tags goto header id3 trim
Controls | media
Originally wanted to write a point of progress control and volume adjustment of the Code, and later found it is too simple, is a few MCI command, back and forth malcontent, I have no interest in writing down. So I thought I'd write something a little more: The reading of music information!

The current mainstream music format is two, MP3 and WMA, they have in the file to save music information in a specific format, MP3 use of course is a well-known ID3 format, divided into V1 and V2 two versions; WMA is the darling of MS, it's just a branch of ASF format, Of course, follow the ASF packaging rules.

How do you get the music information they contain? Generally read their own, of course, XP system began to provide the music file details of the information, the use of FSO can be really connected from the system to read, this is not in the scope of this article, after all, do not control more freedom, better versatility. Therefore, it is necessary to go deep into these kinds of music information format. Or use the code to speak, first to say MP3 ID3v1 and ID3v2 format.

ID3v1 is very simple, a total of 128 bytes, written at the end of the file, the format is as follows:

Private Type Mp3id3v1
Header as String * 3
Title as String * 30
Artist as String * 30
Album as String * 30
Year as String * 4
Comment as String * 30
Genre as String * 1
End Type

ID3v2 is later, the scalability is very strong, written in the file head, using the label group format, divided into two parts, one is the total head of the label group, one is the head of each child label, respectively, defined as follows:

Private Type Mp3id3v2
Header as String * 3
Ver as Byte
Revision as Byte
Flag as Byte
Size (3) as Byte
End Type

Private Type Mp3id3v2tag
Tag as String * 4
Size (3) as Byte
Flag (1) as Byte
End Type

For the convenience of organizing music information, I also defined a structure of my own to facilitate the use of:

' Music type
Private Enum MediaType
Mcimidi = 1
MciMP3 = 2
MCIASF = 4
Mcivideo = 8
Mciwave = 16
End Enum
' The structure of the loading music information
Private Type Musicinfo
FileName as String
Musictype as mediatype
Title as String
Artist as String
Album as String
Year as String
Lyrics as String
Writer as String
Composer as String
Bits as String
Sample as String
Length as Long
End Type

' I'm used to explaining the problem in code, so just look at the code.

Private Function Getmusicinfo (Udtinfo as Musicinfo) as Boolean
Dim strFileName As String, a () As String, I as Long
With Udtinfo
strFileName = Dir (. FileName, vbnormal or Vbhidden or vbreadonly or Vbsystem or vbarchive)
If strFileName = vbnullstring Then Exit Function
. Musictype = Getmcitype (strFileName)
If. Musictype and MciMP3 Then
Getmusicinfo = Getmp3info (udtinfo)
ElseIf. Musictype and mciasf Then
Getmusicinfo = Getasfinfo (udtinfo)
End If
End With
End Function

Private Function Getmcitype (strFileName as String) as mediatype
Dim ext as String
If strFileName <> vbnullstring Then
ext = lcase$ (mid$ (strFileName, InStrRev (strFileName, "."))
Select Case Ext
Case ". mpg", ". Mpeg", ". avi", ". Mpe", ". MPa", ". m1v", ". Ifo", ". VOB"
Getmcitype = Mcivideo
Case ". mp3"
Getmcitype = mciMP3
Case ". wav", ". snd", ". AIF", ". Au", ". aifc", ". Aiff"
Getmcitype = Mciwave
Case ". asf", ". wma", ". Wm", ". WMD"
Getmcitype = mciasf
Case ". wmv"
Getmcitype = mciasf Or mcivideo
Case ". Mid", ". Midi", ". RMI"
Getmcitype = Mcimidi
End Select
End If
End Function


Private Function Getmp3info (Udtinfo as Musicinfo) as Boolean
Dim Freeno as Long, N (1) As Byte, B () as Byte, tmpinfo as Musicinfo
Dim Power as Long, V as Long, j as long, Tagh as Mp3id3v2tag
Dim ID3 as Mp3id3v1, S as String, Pos as Long, id32 as Mp3id3v2
Dim sz as Long, S1 as String
Tmpinfo = Udtinfo
On Error GoTo EXITG
Freeno = FreeFile
Open tmpinfo.filename for Binary as #FreeNo
With Tmpinfo
Pos = LOF (Freeno)-127
If Pos > 0 Then
Get #FreeNo, Pos, ID3
If ucase$ (ID3. Header) = "TAG" Then
s = trim$ (replace$ (ID3). Title, vbNullChar, vbNullString)
If Len (s) > 0 Then
s = replace$ (S, "-", vbNullString)
s = replace$ (S, "--", vbNullString)
s = replace$ (S, ". mp3", vbNullString,,, vbTextCompare)
. Title = S
End If
s = trim$ (replace$ (ID3). Artist, vbNullChar, vbNullString))
If Len (s) > 0 Then
. Title = replace$ (. Title, S, vbNullString)
. Artist = S
End If
s = trim$ (replace$ (ID3). Album, vbNullChar, vbNullString))
If Len (s) > 0 Then. Album = S
s = trim$ (replace$ (ID3). Year, vbNullChar, vbNullString)
If Len (s) > 0 Then. Year = s
End If
End If
Get #FreeNo, 1, id32
If id32. Header = "ID3" Then
SZ = (id32. Size (1) and &h7f) * &h400 + (id32. Size (2) and &h7f) * &h80 + (id32. Size (3) and &h7f)
Pos = sz + 10
S1 = String (4, vbNullChar)
Get #FreeNo, Tagh
Do as not (Tagh.tag = S1 Or Seek (Freeno) > sz + 10)
j = tagh.size (1) * &h10000 + tagh.size (2) * &h100 + tagh.size (3)
If J > 0 Then
ReDim B (j-1)
Get #FreeNo,, b
s = StrConv (b, Vbunicode)
s = trim$ (replace$ (S, vbNullChar, ""))
Select Case Tagh.tag
Case "TIT2"
. Title = S
Case "TPE1"
. Artist = S
Case "Talb"
. Album = S
Case "Tcom"
. Composer = S
Case "TEXT"
. Writer = S
Case "Tyer"
. Year = s
Case "USLT"
s = replace$ (S, "", "")
If lcase$ (left$ (S, 3)) = "Chi" Then
. Lyrics = mid$ (S, 4)
ElseIf lcase$ (left$ (S, 3)) = "Eng" Then
. Lyrics = mid$ (S, 4)
Else
. Lyrics = S
End If
End Select
End If
Get #FreeNo, Tagh
Loop
Else
Pos = 1
End If
Get #FreeNo, Pos, n
SZ = Pos
If not (n (0) = &hff and N (1) >= &AMP;HFA and N (1) <= &hff) Then
Do as not (n (0) = &hff and N (1) = &AMP;HFB)
pos = pos + 1
If Seek (Freeno)-sz > 8192 Then GoTo EXITG
Get #FreeNo, Pos, n
Loop
End If
Get #FreeNo,, n

v = 0
For j = 4 to 7
Power = 2 ^ j
If (n (0) and power) = Power Then v. = v + Power
Next
v = v \ 16
. Bits = trim$ ("144 112 128 160 mid$ 224 256", V * 4 + 1, 4)) & "Kbps"
v = 0
For j = 2 to 3
Power = 2 ^ j
If (n (0) and power) = Power Then v. = v + Power
Next
v = v \ 4
. Sample = trim$ ("mid$", V * 3 + 1, 3)) & "KHz"
End With
Udtinfo = Tmpinfo
Getmp3info = True
EXITG:
Close #FreeNo
End Function

There is also an ASF format, a bit annoying, the next chapter to explain it!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.