Получаем информацию об MP3 файле - VB
Формулировка задачи:
На одном сайте нашел код, очень интересный и тут спрашивали уже такое кстати. Так вот получаем информацию из файла MP3, а именно из его тегов, может пригодится для создания проигрывателя.
И так, создадим "Class Module" с именем clsTagInfo в него следующий код:
Листинг программы
- Option Explicit 'îáúÿâëåГ*ГЁГҐ ïåðåìåГ*Г*ûõ
- Dim sStr As String * 128 ' ñòðîêГ* Гў êîòîðóþ Г§Г*ГЇГЁГёГҐГ¬ ГІГҐГЈ
- Dim HasTag As Boolean ' ГґГ«Г*ГЈ Г*Г*ëè÷èÿ èëè îòñóòñòâèÿ ГІГҐГЈГ*
- Private varFileName As String ' èìÿ ГґГ*éëГ*
- Private Type ID3 ' îáúÿâëåГ*ГЁГҐ ïîëüçîâГ*òåëüñêîãî ГІГЁГЇГ* , îïèñûâГ*ГѕГ№ГҐГЈГ® ГІГҐГЈ
- sTitle As String * 30 ' Г*Г*çâГ*Г*ГЁГҐ ГЇГҐГ±Г*ГЁ
- sArtist As String * 30 ' èñïîëГ*èòåëü
- sAlbum As String * 30 ' Г*ëüáîì
- sYear As String * 4 ' ãîä
- sComment As String * 28 ' êîììåГ*ГІГ*ðèé
- sTrack(1) As Byte ' Г*îìåð òðåêГ*
- sGenre As Byte ' Г¦Г*Г*Г°
- End Type
- Dim info As ID3
- Private Function Read() ' ГґГіГ*êöèÿ ñ÷èòûâГ*ГѕГ№Г*Гї ГІГҐГЈ
- Dim nFile As Integer ' Г*îìåð ГґГ*éëГ*
- Dim Tag As String * 3
- On Error Resume Next ' Г*ГҐ îñòГ*Г*Г*âëèâГ*ГІГј ïðîãðГ*ììó ïðè îøèáêå
- nFile = FreeFile ' ïîëó÷Г*ГҐГ¬ Г*îìåð ñâîáîäГ*îãî ГґГ*éëГ*
- Open varFileName For Binary As nFile ' îòêðûâГ*ГҐГ¬ ГґГ*éë varFileName äëÿ ïîáГ*éòîâîãî äîñòóïГ* ïîä Г*îìåðîì nFile
- Get nFile, LOF(nFile) - 127, sStr ' Г§Г*ïèñûâГ*ГҐГ¬ 127 ïîñëåäГ*ГЁГµ ñèìâîëîâ ГґГ*éëГ* Гў ïåðåìåГ*Г*ГіГѕ sStr
- Close nFile ' Г§Г*êðûâГ*ГҐГ¬ ГґГ*éë
- Tag = Mid(sStr, 1, 3) ' ГЁГ№ГҐГ¬ ìåòêó ГІГҐГЈГ* ID3v1
- If Tag = "TAG" Then ' åñëè ìåòêГ* ГҐГ±ГІГј
- HasTag = True ' ïðèñâГ*ГЁГўГ*ГҐГ¬ ГґГ«Г*ГЈГі ÈñòèГ*Г*
- Else
- HasTag = False ' åñëè Г*ГҐГІ ìåòêè ГІГ® ГґГ«Г*ГЈ-Г”Г*ëüøü
- End If
- If HasTag = True Then ' Г°Г*çáèâГ*ГҐГ¬ ГІГҐГЈ Г*Г* îòäåëüГ*ûå ñîñòГ*âëÿþùèå
- info.sTitle = Mid(sStr, 4, 30)
- info.sArtist = Mid(sStr, 34, 30)
- info.sAlbum = Mid(sStr, 64, 30)
- info.sYear = Mid(sStr, 94, 4)
- info.sComment = Mid(sStr, 98, 28)
- info.sTrack(1) = Asc(Mid(sStr, 127, 1))
- info.sGenre = Asc(Mid(sStr, 128, 1))
- End If
- End Function
- Public Function Save() ' ГґГіГ*êöèÿ Г§Г*ïèñûâГ*ГѕГ№Г*Гї Гў ГґГ*éë Г*îâûé ГІГҐГЈ
- Dim nFile
- info.sTrack(0) = 0
- nFile = FreeFile
- If HasTag = True Then ' åñëè òåã åñòü
- Open varFileName For Binary As nFile ' îòêðûâГ*ГҐГ¬ ГґГ*éë
- Put nFile, LOF(nFile) - 124, info ' Г§Г*ïèñûâГ*ГҐГ¬ ГІГҐГЈ
- Close nFile ' Г§Г*êðûâГ*ГҐГ¬ ГґГ*éë
- Else ' åñëè ГІГҐГЈГ* Г*ГҐГІ
- Open varFileName For Binary As nFile ' îòêðûâГ*ГҐГ¬ ГґГ*éë
- Put nFile, LOF(nFile) - 127, "TAG" ' Г§Г*ïèñûâГ*ГҐГ¬ ìåòêó ГІГҐГЈГ*
- Close nFile ' Г§Г*êðûâГ*ГҐГ¬ ГґГ*éë
- Call Save ' âûïîëГ*ГїГҐГ¬ ðåêóðñèþ
- End If
- End Function
- ' äëÿ äîñòóïГ* ГЄ ГЄГ«Г*Г±Г±Гі èñïîëüçóåì ïðîöåäóðû ñâîéñòâ Property Get ГЁ Property Let
- Public Property Get Title() As String
- Title = Trim(info.sTitle)
- End Property
- Public Property Let Title(ByVal vNewValue As String)
- info.sTitle = vNewValue
- End Property
- Public Property Get FileName() As String
- End Property
- Public Property Let FileName(ByVal vNewValue As String)
- varFileName = vNewValue
- Read
- End Property
- Public Property Get Artist() As String
- Artist = Trim(info.sArtist)
- End Property
- Public Property Let Artist(ByVal vNewValue As String)
- info.sArtist = vNewValue
- End Property
- Public Property Get Album() As String
- Album = Trim(info.sAlbum)
- End Property
- Public Property Let Album(ByVal vNewValue As String)
- info.sAlbum = vNewValue
- End Property
- Public Property Get Year() As String
- Year = Trim(info.sYear)
- End Property
- Public Property Let Year(ByVal vNewValue As String)
- info.sYear = vNewValue
- End Property
- Public Property Get Comment() As String
- Comment = Trim(info.sComment)
- End Property
- Public Property Let Comment(ByVal vNewValue As String)
- info.sComment = vNewValue
- End Property
- Public Property Get Track() As Variant
- Track = info.sTrack(1)
- If Track = 0 Then Track = ""
- End Property
- Public Property Let Track(ByVal vNewValue As Variant)
- If Not IsNumeric(vNewValue) Or vNewValue > 255 Then
- MsgBox "Ââåäèòå öèôðó Г*ГҐ áîëüøå 255"
- Else
- info.sTrack(1) = vNewValue
- End If
- End Property
- Public Property Get Genre() As Variant
- Genre = info.sGenre
- End Property
- Public Property Let Genre(ByVal vNewValue As Variant)
- info.sGenre = vNewValue
- End Property
Далее в форму код:
Листинг программы
- Dim info As clsTagInfo
- Set info = New clsTagInfo
- info.FileName = "ПУТЬ К ФАЙЛУ MP3"
- cboGenre.ListIndex = info.Genre
- txtAlbum.Text = info.Album
- txtArtist.Text = info.Artist
- txtComment.Text = info.Comment
- txtTitle.Text = info.Title
- txtTrack.Text = info.Track
- txtYear.Text = info.Year
Ну и еще короче:
Листинг программы
- Private Sub Form_Load()
- Dim fNum As Integer
- Dim sTagIdent As String * 3
- Dim sTitle As String * 30
- Dim sArtist As String * 30
- Dim sAlbum As String * 30
- Dim sYear As String * 4
- Dim sComment As String * 30
- fNum = FreeFile
- 'Замените ярлык 'c:\MySong.mp3' любым вашим файлом.
- Open "c:\MySong.mp3" For Binary As fNum
- Seek #fNum, LOF(fNum) - 127
- Get #fNum, , sTagIdent
- If sTagIdent = "TAG" Then
- Get #fNum, , sTitle
- Get #fNum, , sArtist
- Get #fNum, , sAlbum
- Get #fNum, , sYear
- Get #fNum, , sComment
- End If
- Close #fNum
- MsgBox sTitle & "," & sArtist & "," & sAlbum & "," & sYear & "," & sComment
- End Sub
В первом варианте, можно создать свой .DLL файлик.
Решение задачи: «Получаем информацию об MP3 файле»
textual
Листинг программы
- format(369.5/86400,"h:mm:ss")
- format(timeserial(0,0,369.5),"h:mm:ss")
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д