欢迎光临
我们一直在努力

无外部控件制作多媒体播放器(二)-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

本来想写点进度控制与音量调整的代码的,后来发现还是太简单了,就是几个mci命令,来回搬弄,自己都没兴趣写下去。所以我想还是写些独门一点的:音乐信息的读取!

目前常见的主流音乐格式就两种,mp3与wma,它们都有在文件中保存音乐信息的特定格式,mp3使用的当然是家喻户晓的id3格式,分为v1与v2两个版本;wma是ms的宠儿,它只是asf格式的一个分支,当然遵循asf的包装规则。

怎么获取它们包含的音乐信息呢?一般是自己读取,当然xp系统开始提供了音乐文件的详细信息资料,利用fso可以真接从系统那里读取到,这不在本文范围,毕竟不用控件更自由,通用性更好。所以,有必要去深入一下这几种音乐信息格式。还是用代码说话吧,先说说mp3的id3v1与id3v2的格式。

id3v1很简单,共128个字节,写在文件尾部,格式如下:

private type mp3id3v1
    header as string * 3
    title as string * 30
    artist as string * 30
    album as string * 30
    year as string * 4
    comment as string * 30
    genre as string * 1
end type

id3v2是后来出现的,可扩展性很强,写在文件头部,采用标签组格式,分两部分,一是标签组的总头部,一是每个子标签的头部,分别定义如下:

private type mp3id3v2
    header as string * 3
    ver as byte
    revision as byte
    flag as byte
    size(3) as byte
end type

private type mp3id3v2tag
    tag as string * 4
    size(3) as byte
    flag(1) as byte
end type

为了组织音乐信息的方便,我还定义了一个自己的结构,以便于使用:

音乐类型
private enum mediatype
    mcimidi = 1
    mcimp3 = 2
    mciasf = 4
    mcivideo = 8
    mciwave = 16
end enum
装载音乐信息的结构
private type musicinfo
    filename as string
    musictype as mediatype
    title as string
    artist as string
    album as string
    year as string
    lyrics as string
    writer as string
    composer as string
    bits as string
    sample as string
    length as long
end type

我习惯于用代码说明问题,所以还是看看代码吧

private function getmusicinfo(udtinfo as musicinfo) as boolean
    dim strfilename as string, a() as string, i as long
    with udtinfo
        strfilename = dir(.filename, vbnormal or vbhidden or vbreadonly or vbsystem or vbarchive)
        if strfilename = vbnullstring then exit function
        .musictype = getmcitype(strfilename)
        if .musictype and mcimp3 then
            getmusicinfo = getmp3info(udtinfo)
        elseif .musictype and mciasf then
            getmusicinfo = getasfinfo(udtinfo)
        end if
    end with
end function

private function getmcitype(strfilename as string) as mediatype
    dim ext as string
    if strfilename <> vbnullstring then
        ext = lcase$(mid$(strfilename, instrrev(strfilename, ".")))
        select case ext
            case ".mpg", ".mpeg", ".avi", ".mpe", ".mpa", ".m1v", ".ifo", ".vob"
                getmcitype = mcivideo
            case ".mp3"
                getmcitype = mcimp3
            case ".wav", ".snd", ".aif", ".au", ".aifc", ".aiff"
                getmcitype = mciwave
            case ".asf", ".wma", ".wm", ".wmd"
                getmcitype = mciasf
            case ".wmv"
                getmcitype = mciasf or mcivideo
            case ".mid", ".midi", ".rmi"
                getmcitype = mcimidi
        end select
    end if
end function

private function getmp3info(udtinfo as musicinfo) as boolean
    dim freeno as long, n(1) as byte, b() as byte, tmpinfo as musicinfo
    dim power as long, v as long, j as long, tagh as mp3id3v2tag
    dim id3 as mp3id3v1, s as string, pos as long, id32 as mp3id3v2
    dim sz as long, s1 as string
    tmpinfo = udtinfo
    on error goto exitg
    freeno = freefile
    open tmpinfo.filename for binary as #freeno
    with tmpinfo
        pos = lof(freeno) – 127
        if pos > 0 then
            get #freeno, pos, id3
            if ucase$(id3.header) = "tag" then
                s = trim$(replace$(id3.title, vbnullchar, vbnullstring))
                if len(s) > 0 then
                    s = replace$(s, "-", vbnullstring)
                    s = replace$(s, "——", vbnullstring)
                    s = replace$(s, ".mp3", vbnullstring, , , vbtextcompare)
                    .title = s
                end if
                s = trim$(replace$(id3.artist, vbnullchar, vbnullstring))
                if len(s) > 0 then
                    .title = replace$(.title, s, vbnullstring)
                    .artist = s
                end if
                s = trim$(replace$(id3.album, vbnullchar, vbnullstring))
                if len(s) > 0 then .album = s
                s = trim$(replace$(id3.year, vbnullchar, vbnullstring))
                if len(s) > 0 then .year = s
            end if
        end if
        get #freeno, 1, id32
        if id32.header = "id3" then
            sz = (id32.size(1) and &h7f) * &h400 + (id32.size(2) and &h7f) * &h80 + (id32.size(3) and &h7f)
            pos = sz + 10
            s1 = string(4, vbnullchar)
            get #freeno, , tagh
            do while not (tagh.tag = s1 or seek(freeno) > sz + 10)
                j = tagh.size(1) * &h10000 + tagh.size(2) * &h100 + tagh.size(3)
                if j > 0 then
                    redim b(j – 1)
                    get #freeno, , b
                    s = strconv(b, vbunicode)
                    s = trim$(replace$(s, vbnullchar, ""))
                    select case tagh.tag
                    case "tit2"
                        .title = s
                    case "tpe1"
                        .artist = s
                    case "talb"
                        .album = s
                    case "tcom"
                        .composer = s
                    case "text"
                        .writer = s
                    case "tyer"
                        .year = s
                    case "uslt"
                        s = replace$(s, "  ", " ")
                        if lcase$(left$(s, 3)) = "chi" then
                            .lyrics = mid$(s, 4)
                        elseif lcase$(left$(s, 3)) = "eng" then
                            .lyrics = mid$(s, 4)
                        else
                            .lyrics = s
                        end if
                    end select
                end if
                get #freeno, , tagh
            loop
        else
            pos = 1
        end if
        get #freeno, pos, n
        sz = pos
        if not (n(0) = &hff and n(1) >= &hfa and n(1) <= &hff) then
            do while not (n(0) = &hff and n(1) = &hfb)
                pos = pos + 1
                if seek(freeno) – sz > 8192 then goto exitg
                get #freeno, pos, n
            loop
        end if
        get #freeno, , n
           
        v = 0
        for j = 4 to 7
            power = 2 ^ j
            if (n(0) and power) = power then v = v + power
        next
        v = v \ 16
        .bits = trim$(mid$("144 320 32  48  56  64  80  96  112 128 160 192 224 256 320 ", v * 4 + 1, 4)) & "kbps"
        v = 0
        for j = 2 to 3
            power = 2 ^ j
            if (n(0) and power) = power then v = v + power
        next
        v = v \ 4
        .sample = trim$(mid$("44 48 32 ?? ", v * 3 + 1, 3)) & "khz"
    end with
    udtinfo = tmpinfo
    getmp3info = true
exitg:
    close #freeno
end function

还有个asf格式,有点烦,下篇再做说明吧!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 无外部控件制作多媒体播放器(二)-.NET教程,Asp.Net开发
分享到: 更多 (0)