以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  读取mp3 ID3 信息的.NET 代码  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=11783)


--  作者:admin
--  发布时间:11/9/2004 2:25:00 AM

--  读取mp3 ID3 信息的.NET 代码


发信人: sEELa (sEELa), 信区: DotNET
标  题: 读取mp3 ID3 信息的.NET 代码
发信站: BBS 水木清华站 (Mon Dec 29 16:05:14 2003), 转信

这个是完整的能够读取mp3 id3v1的.net 代码
修改了一下可以支持中文了
原来的代码在byteArrayToString这个函数中用ASCII进行byte到char的转换是不对
的,会不能正常显示中文
应该用default或者干脆用GB2312(winamp好像是这个编码存储 Id3的)



'/****************************************************************************
******************************************************************************
************************
' * This work is based on an original GPL project for a Java version of this
ID3 tag parser.  The Java version is available on request.
' * If you are interested in a C# version there are plenty of them floating
around the internet (that's why we did this one in VB.NET) and
' * it should be easy enough to port this over to C# if you want to do so.
' * This work is LGPL software and those rights and restrictions transfer to
you.
' *
' * In simple terms and to the best of our understanding the LGPL License
means the following to you:
'* ---------------------------------------------------------------------------
----------------------
'* 1 - You CAN use this library in proprietary software without affecting
your rights to protect YOUR software.
'*         When we use Open Source software we only use LGPL as it is the
only (our opinion) Open Source software license that protects
'*     the right to protect your closed source software while still taking
advantage of Open Source Software.
'*
'* 2 - When you distribute your application that has this library or a
derivative in it you are to provide the ability to get the source code
'*     to this library or your derivative.  How you do that is your
business; you can require your customers to request it, you can provide it
with
'*     your application, you can provide a place to download it, whatever
YOU want.
'*
'* 3 - You must indicate if you have changed the library to provide
protection for the original and subsequent authors/modifiers
'*     reputation.  So if you break it or hack it in to some really junky
code the original author isn't blamed for it
'*
'* For More Detailed Information about this License Review the most Current
LGPL License Agreement here:

'* http://www.opensource.org/licenses/lgpl-license.php
'*
'*
'* Again, most of our components are C#; this one was done in VB.NET as at
the time we created this there weren't any examples available in VB.NET
'*
'* ***************************************************************************
******************************************************************************
*************************/

Imports System
Imports System.IO
Imports System.Text

Namespace Ambientware.multimedia
    Public Class ID3Util
        Private songTitle As String
        Private artist As String
        Private albumTitle As String
        Private year As String
        Private comment As String
        Private cdTrack As Integer
        Private genre As String
        Private mp3 As FileStream
        Private id3StartV1 As Long
        Private id3StartV2 As Long
        Private id3Version As Integer
        Private id3Tag(128) As Byte
        Private completeTag As String


        Sub New(ByVal mp3FilePath As String)
            mp3 = File.OpenRead(mp3FilePath)
            id3StartV1 = mp3.Length() - 128
            id3StartV2 = 0
            setVersion()
            populateFields()
            mp3.Close()
        End Sub

        'Retrieves the song title from the id3 tag
        'return A string representing the song title
        Public Function getSongTitle() As String
            Return songTitle
        End Function

        'Gets the name of the artist
        'return A string representing the artist name
        Public Function getArtist() As String
            Return (artist)
        End Function



        'Get the name of the album
        'return A string representing the name of the album the mp3 is from.
        Public Function getAlbumTitle() As String
            Return (albumTitle)
        End Function


        'Get the year the song was recorded.
        'return A string representing the year the song was recorded.
        Public Function getYear() As String
            Return (year)
        End Function


        'Get the comment, if any, from the id3 tag
        'return A string representing the comment portion of the id3 tag
        Public Function getComment() As String
            Return (comment)
        End Function


        'Get the track number from the cd
        'return The track number from the album.
        Public Function getCdTrack() As String
            If (cdTrack = -1) Then
                Return ("Unknown")
            Else
                Return (cdTrack.ToString())
            End If
        End Function


        'Get the genre of the song
        'return A string value representing the content genre, if available,
of the mp3.
        Public Function getGenre() As String
            Return (genre)
        End Function



        Private Function populateFields()
            Dim genres As String() = {"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", "AlternRock", "Bass", "Soul", "Punk", "Space",
"Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic",
"Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance",
"Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian
Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave",
"Psychedelic", "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", "Bluegrass", "Avantgarde", "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 Brass",
"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
"Folklore", "Ballad", "Power Ballad", "Rhytmic Soul", "Freestyle", "Duet",
"Punk Rock", "Drum Solo", "Acapella", "Euro-House", "Dance Hall"}

            songTitle = byteArrayToString(id3Tag, 3, 30)
            If (Not verify(songTitle)) Then
                songTitle = "Unknown"
            End If

            artist = byteArrayToString(id3Tag, 33, 30)
            If (Not verify(artist)) Then

                artist = "Unknown"
            End If

            albumTitle = byteArrayToString(id3Tag, 63, 30)
            If (Not verify(albumTitle)) Then
                albumTitle = "Unknown"
            End If

            year = byteArrayToString(id3Tag, 93, 4)
            If (Not verify(year)) Then
                year = "Unknown"
            End If

            comment = byteArrayToString(id3Tag, 97, 30)
            If (Not verify(comment)) Then
                comment = "Unknown"

            End If

            cdTrack = id3Tag(126)
            If (cdTrack <= 0) Then
                cdTrack = -1
            End If

            If ((id3Tag(127) >= 0) And (id3Tag(127) <= 125)) Then
                genre = genres(id3Tag(127))
            Else
                genre = "Unknown"
            End If

            completeTag = "SongTitle: " + songTitle + "\nArtist: " + artist
+ "\nAlbumTitle: " + albumTitle + "\nYear: " + year + "\nComment: " +
comment + "\nTrack# " + getCdTrack() + "\nGenre: " + genre
        End Function


        'working function to return the string from the varios byte arrays
        'that make up the ID3 tags in the MP3 files

        Private Function byteArrayToString(ByVal arrByte() As Byte, ByVal
intBeginPOS As Integer, ByVal intLength As Integer) As String

            Dim d As System.Text.Decoder = System.Text.Encoding.Default.GetDec
oder
            Dim chr(128) As Char
            d.GetChars(arrByte, intBeginPOS, intLength, chr, 0)
            Dim p As Integer
            For p = 0 To intLength - 1
                If chr(p) = vbNullChar Then

                    Exit For
                Else
                    byteArrayToString &= chr(p)
                End If
            Next
            Return byteArrayToString



        End Function



        Private Shared Function verify(ByVal field As String) As Boolean
            Return True
        End Function


        'Determine which version of the ID3 tags we are dealing with
        Private Function setVersion()
            Dim type As String = ""
            Dim strTmp As String = ""

            Dim X As Integer

            mp3.Position = id3StartV1
            mp3.Read(id3Tag, 0, 128)
            strTmp = Me.byteArrayToString(id3Tag, 0, 3)
            If (strTmp.ToUpper.Equals("TAG")) Then 'Then we've got a version
one
                id3Version = 1
            Else 'We've got version two
                mp3.Position = id3StartV2
                mp3.Read(id3Tag, 0, 128)

                strTmp = Me.byteArrayToString(id3Tag, 0, 3)

                If (strTmp.ToUpper.Equals("ID3")) Then
                    id3Version = 2
                Else
                    id3Version = -1
                End If

            End If

            If (id3Version = 1) Then
                mp3.Position = id3StartV1
                mp3.Read(id3Tag, 0, 128)
            Else
                mp3.Position = id3StartV2
                mp3.Read(id3Tag, 0, 128)
            End If
            '          Dim tmp As System.Text.Encoding
            '         id3Tag = System.Text.Encoding.Convert(System.Text.Encodi
ng.ASCII, System.Text.Encoding.Default, id3Tag)
        End Function


        'Get the id3tag version.
        'return The id3 verison of the tag (1 or 2)
        Public Function getID3Version() As Integer
            Return (id3Version)
        End Function


        'Get the complete id3tag
        'return A string representing the complete id3 tag as a
concantenation of all the values.
        Public Function getCompleteTag() As String
            If (id3Version = 1) Then
                Return (completeTag)
            Else
                Return ("Not a valid tag")
            End If
        End Function

    End Class

End Namespace

--

※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.87.***]
上一篇
返回上一页
回到目录
回到页首



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms