新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → 读取mp3 ID3 信息的.NET 代码 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2905 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 读取mp3 ID3 信息的.NET 代码 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 读取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.***]
    上一篇
    返回上一页
    回到目录
    回到页首


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 6:55:17

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms