匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 1 0 年前

VB 如何取的一首mp3的tag資訊?

有人知道如何得到一首mp3的tag資訊嗎?

VB要如何做到呢

VB要如何做到呢

VB要如何做到呢

1 個解答

評分
  • 1 0 年前
    最佳解答

    Attribute VB_Name = "Mp3_BAS"

    Rem HaveID3 -> 傳回這個檔案是否含有 ID3標籤

    Function HaveID3(FileName As String) As Boolean

    On Error Resume Next

    Dim a(128) As Byte

    Rem 開啟檔案

    Open FileName For Binary As #100

    Seek #100, FileLen(FileName) - 128 + 1

    Get #100, , a

    Close #100

    Rem 判斷是否有"TAG"

    If a(0) = 84 And a(1) = 65 And a(2) = 71 Then HaveID3 = True

    End Function

    Rem 將讀進的Byte陣列轉成String,不支援換行,用到硬碟寫入..

    Function ConByteToStr(ByRef aBy() As Byte) As String

    On Error Resume Next

    Dim Tempstr As String

    Kill "c:\abc"

    Open "c:\abc" For Binary As #20

    Put #20, , aBy

    Close #20

    Open "c:\abc" For Input As #20

    Do While Not EOF(20)

    Line Input #20, Tempstr

    ConByteToStr = ConByteToStr + Tempstr

    Loop

    Close #20

    Kill "c:\abc"

    End Function

    Rem 計算字串長度

    Function MyLen(ByVal Str As String) As Long

    MyLen = LenB(StrConv(Str, vbFromUnicode))

    End Function

    Rem 將字串補足到某一長度

    Sub StrPlus(ByRef StrToPlus As String, ByVal PlusStr As String, ByVal PlusTo As Long)

    On Error Resume Next

    StrToPlus = StrToPlus + String(PlusTo - MyLen(StrToPlus), PlusStr)

    End Sub

    ' 資料來源 :www.vbguide.com.tw

    ' 如果一個檔案包含了 ID3-Tag 的話,其最後 128 個 bytes 會如以下所說明的:

    '------------------------------------------------------------------------

    ' 資訊內容 資料型態 長度

    '----------------------------------

    ' "TAG" String 3

    ' Title String 30

    ' Artist String 30

    ' Album String 30

    ' Year String 4

    ' Comment String 30

    ' Genre-Identifier Byte 1

    '---------------------------------

    2007-11-25 07:54:22 補充:

    Sub ReadID3(ByVal Mp3Filename As String, Optional ByRef Title As String, Optional ByRef Artist As String, Optional ByRef Album As String, Optional ByRef Year As String, Optional ByRef Comment As String, Optional ByRef GenreIdentifier As Byte)

    If HaveID3(Mp3Filename) Then '有ID3

    2007-11-25 07:55:05 補充:

    Rem 開啟

    Dim TitleB(29) As Byte

    Dim ArtisB(29) As Byte

    Dim AlbumB(29) As Byte

    Dim YearB(3) As Byte

    Dim CommeB(29) As Byte

    Dim GIdenB(0) As Byte

    2007-11-25 07:55:17 補充:

    Open Mp3Filename For Binary As #5

    Seek #5, FileLen(Mp3Filename) - 128 + 1 + 3

    Get #5, , TitleB

    Get #5, , ArtisB

    Get #5, , AlbumB

    Get #5, , YearB

    Get #5, , CommeB

    Get #5, , GIdenB

    Close #5

    2007-11-25 07:55:39 補充:

    Title = Trim(ConByteToStr(TitleB))

    Artist = Trim(ConByteToStr(ArtisB))

    Album = Trim(ConByteToStr(AlbumB))

    Year = Trim(ConByteToStr(YearB))

    Comment = Trim(ConByteToStr(CommeB))

    GenreIdentifier = GIdenB(0)

    2007-11-25 07:55:48 補充:

    Debug.Print Title

    Debug.Print Artist

    Debug.Print Album

    Debug.Print Year

    Debug.Print Comment

    Debug.Print GenreIdentifier

    Else

    Title = ""

    Artist = ""

    Album = ""

    Year = ""

    Comment = ""

    GenreIdentifier = 0

    End If

    End Sub

    2007-11-25 07:56:04 補充:

    Function WriteID3(ByVal Mp3Filename As String, ByVal Title As String, ByVal Artist As String, ByVal Album As String, ByVal Year As String, ByVal Comment As String, ByVal GenreIdentifier As Byte)

    Rem 先將ID3做成字串

    2007-11-25 07:56:22 補充:

    Call StrPlus(Title, " ", 30)

    Call StrPlus(Artist, " ", 30)

    Call StrPlus(Album, " ", 30)

    Call StrPlus(Year, " ", 4)

    Call StrPlus(Comment, " ", 30)

    Dim Total As String

    Total = Title + Artist + Album + Year + Comment

    If HaveID3(Mp3Filename) Then '有ID3

    Rem 開啟

    Open Mp3Filename For Binary As #5

    2007-11-25 07:56:41 補充:

    Seek #5, FileLen(Mp3Filename) - 128 + 1 + 3

    Put #5, , Total

    Put #5, , GenreIdentifier

    Close #5

    Else

    Total = "TAG" + Total

    Rem 開啟

    Open Mp3Filename For Binary As #5

    2007-11-25 07:57:01 補充:

    Seek #5, FileLen(Mp3Filename)

    Put #5, , Total

    Put #5, , GenreIdentifier

    Close #5

    End If

    End Function

    2007-11-25 07:58:47 補充:

    因為字數限制的關係,我分成好幾段發出,造成你的麻煩請見諒

    參考資料: http://vb.ncis.com.tw/ VB研究小站
還有問題?馬上發問,尋求解答。