Originally wanted to write a code of progress control and volume adjustment, and later found too simple, just a few MCI commands, back and forth, and they were not interested in writing. So I think I still write some unique: reading of music information!
At present, the common mainstream music format is two kinds, MP3 and WMA, which all have specific formats for saving music information in the file, of course, of course, is a household name ID3 format, divided into V1 and V2; WMA is MS Par, it is just a branch in the ASF format, of course, follow the ASF packaging rules.
How to get the music information they contain? It is generally read, and of course the XP system begins to provide a detailed information of music files. Using FSO can really read from the system there, which is not hereby, after all, no control is more free, more versatility. Therefore, it is necessary to go deep into these music information formats. Still talking with the code, first talk about the format of the ID3v1 and ID3v2 of MP3.
ID3V1 is simple, a total of 128 bytes, written in 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 * 1nd TypeID3V2 is later, scalability, Written on the file header, using the label group format, divided into two parts, one is the total head of the label group, one is the head of each sub-tag, the definition is as follows:
Private type mp3id3v2 header as string * 3 ver as byte revision as byte flag as byte size (3) as byteend typeprivate type mp3id3v2tag tag as string * 4 size (3) as byte flag (1) as byteend type
In order to organize the convenience of music information, I also define a structure in order to use:
'Style Private Enum MediaType mciMIDI = 1 mciMP3 = 2 mciASF = 4 mciVIDEO = 8 mciWAVE = 16End Enum' load music information structure 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 LONGEND TYPE
'I am used to explain the code, so I still 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 WithEnd FunctionPrivate Function GetMCIType (strFileName As String) As MediaType Dim ext As String If StrfileName <> vbnullstring kiln = lcase $ (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 IFEND 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 $ (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 $ (iD3.album, vbnullchar, vbnullstring)) if len (s)> 0 thr, = s = Trim $ (id3.year, vbnullchar, vbnullstring) ife (s)> 0 dam .year = s end if endiff #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 while 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 $ (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 endiff # Freeno, TAGH LOOP ELSE POS = 1 Endiff Get #Freeno, POS, N SZ = POS IF NOT (N (0) = & HFF and N (1)> = & HFA and N (1) <= &
HFF) THEN Do while not (n (0) = & hff and n (1) = & hfb) POS = POS 1 if Seek (freeeno) - SZ> 8192 Then Goto EXITG GET #Freeno, Pos, N loop endiff end g Get # Freeno,, N v = 0 for J = 4 to 7 Power = 2 ^ J IF (n (0) and power) = Power Ten v = V Power next v = v / 16.bits = TRIM $ (MID $ "144 320 32 48 56 64 80 96 112 128 160" V * 4 1, 4)) & "kbps" v = 0 for j = 2 to 3 Power = 2 ^ j = (n (0 ) And power = Power then v = v power next v = v / 4 .sample = trim $ ("44 48 32 ??", v * 3 1, 3)) & "kHz" end with Udtinfo = tmpinfo getmp3info = truexitg: Close #freenoend function also has an ASF format, it is a bit annoying, the next article is noted!