VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • vb教程之得到MP3中隐藏的信息

    为了保护音乐者的版权,在MP3文件中有一段特意存储版权说明的信息,众多的MP3播放器软件都提供了修改和读取MP3文件中隐藏信息的功能,那么这些信息到底存储在哪里呢?如何得到这些信息呢?本文将为大家提供得到MP3信息的模块。

      首先,定义一个公共类型Mp3tag如下:

  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
    
    Seek FileNum, LOF(FileNum) - 127             ’把文件指针移动到MP3信息处
    strInput = Space(3)
    Get FileNum, , strInput


    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)
        
    Close FileNum

End Function
   
   注意:MP3文件对音乐的风格进行了限制,共254种。Genre返回的只是MP3风格的序列号,具体还需要定位,在这里我把所有类型以常数形式列出,每个类型之间用"|"号隔开。

Private Const sGenreMatrix = "Blues|Classic Rock|Country|Dance|Disco|Funk|Grunge|" + _
    "Hip-Hop|Jazz|Metal|New Age|Oldies|Other|Pop|R&B|Rap|Reggae|Rock|Techno|" + _
    "Industrial|Alternative|Ska|Death Metal|Pranks|Soundtrack|Euro-Techno|" + _
    "Ambient|Trip Hop|Vocal|Jazz+Funk|Fusion|Trance|Classical|Instrumental|Acid|" + _
    "House|Game|Sound Clip|Gospel|Noise|Alt. Rock|Bass|Soul|Punk|Space|Meditative|" + _
    "Instrumental Pop|Instrumental Rock|Ethnic|Gothic|Darkwave|Techno-Industrial|Electronic|" + _
    "Pop-Folk|Eurodance|Dream|Southern Rock|Comedy|Cult|Gangsta Rap|Top 40|Christian Rap|" + _
    "Pop/Punk|Jungle|Native American|Cabaret|New Wave|Phychedelic|Rave|Showtunes|Trailer|" + _
    "Lo-Fi|Tribal|Acid Punk|Acid Jazz|Polka|Retro|Musical|Rock & Roll|Hard Rock|Folk|" + _
    "Folk/Rock|National Folk|Swing|Fast-Fusion|Bebob|Latin|Revival|Celtic|Blue Grass|" + _
    "Avantegarde|Gothic Rock|Progressive Rock|Psychedelic Rock|Symphonic Rock|Slow Rock|" + _
    "Big Band|Chorus|Easy Listening|Acoustic|Humour|Speech|Chanson|Opera|Chamber Music|" + _
    "Sonata|Symphony|Booty Bass|Primus|Porn Groove|Satire|Slow Jam|Club|Tango|Samba|Folklore|" + _
    "Ballad|power Ballad|Rhythmic Soul|Freestyle|Duet|Punk Rock|Drum Solo|A Capella|Euro-House|" + _
    "Dance Hall|Goa|Drum & Bass|Club-House|Hardcore|Terror|indie|Brit Pop|Negerpunk|Polsk Punk|" + _
    "Beat|Christian Gangsta Rap|Heavy Metal|Black Metal|Crossover|Comteporary Christian|" + _
    "Christian Rock|Merengue|Salsa|Trash Metal|Anime|JPop|Synth Pop"


    把以上代码写到一个模块中(.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
            End Sub
  
     以上代码在VB6 \Win2000中测试通过。

相关教程