C# 获取 MP3信息结构 的一个小例子(基于v1版的ID3)

ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版。其中:v1版的ID3在mp3文件的末尾128字节,以TAG三个字符开头,后面跟上歌曲信息。v2版一般位于mp3的开头,可以存储歌词,该专辑的图片等大容量的信息。

    using System.IO;
    using System.Text;

    public class Mp3
    {
        #region MP3信息结构
        /// <summary>
        /// MP3信息结构
        /// </summary>
        public struct Mp3Info
        {
            public string identify; //TAG,三个字节
            public string Title; //歌曲名,30个字节
            public string Artist; //歌手名,30个字节
            public string Album; //所属唱片,30个字节
            public string Year; //年,4个字符
            public string Comment; //注释,28个字节
            public char reserved1; //保留位,一个字节
            public char reserved2; //保留位,一个字节
            public char reserved3; //保留位,一个字节
        }
        #endregion

        public static Mp3Info GetMp3Info(string FileName)
        {
            //打开文件
            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            //获取编码
            Encoding FileEncoding = Text.Encoding.Main.GetEncoding(fs);
            //获取MP3文件最后128个字节,ID3信息保存于此,如果获取失败,则返回null
            const int seekPos = 128;
            fs.Seek(-seekPos, SeekOrigin.End); //从文件尾部开始往回seek到128字节处
            int rl = 0;
            byte[] Last128 = new byte[seekPos];
            rl = fs.Read(Last128, 0, seekPos); //将最后的128个字节读出来放入byte[]中
            fs.Seek(0, SeekOrigin.Begin);  //恢复Seek位置
            //关闭文件
            fs.Close();

            //将mp3最后的128个字节格式化为Mp3Info
            Mp3Info myMp3Info = FormatMp3Info(Last128, FileEncoding);
            //返回
            return myMp3Info;
        }

        #region 将mp3最后的128个字节格式化为Mp3Info
        /// <summary>
        /// 将mp3最后的128个字节格式化为Mp3Info
        /// </summary>
        /// <param name = "Info">从MP3文件中截取的二进制信息</param>
        /// <returns>返回一个Mp3Info结构</returns>
        private static Mp3Info FormatMp3Info(byte[] Info, System.Text.Encoding Encoding)
        {
            Mp3Info myMp3Info = new Mp3Info();
            string str = null;
            int i;
            int position = 0; //循环的起始值
            int currentIndex = 0; //Info的当前索引值

            //获取TAG标识
            for (i = currentIndex; i < currentIndex + 3; i++)
            {
                str = str + (char)Info[i];
                position++;
            }
            currentIndex = position;
            myMp3Info.identify = str;

            //获取歌名
            str = null;
            byte[] bytTitle = new byte[30]; //将歌名部分读到一个单独的数组中
            int j = 0;
            for (i = currentIndex; i < currentIndex + 30; i++)
            {
                bytTitle[j] = Info[i];
                position++;
                j++;
            }
            currentIndex = position;
            myMp3Info.Title = ByteToString(bytTitle, Encoding);

            //获取歌手名
            str = null;
            j = 0;
            byte[] bytArtist = new byte[30]; //将歌手名部分读到一个单独的数组中
            for (i = currentIndex; i < currentIndex + 30; i++)
            {
                bytArtist[j] = Info[i];
                position++;
                j++;
            }
            currentIndex = position;
            myMp3Info.Artist = ByteToString(bytArtist, Encoding);

            //获取唱片名
            str = null;
            j = 0;
            byte[] bytAlbum = new byte[30]; //将唱片名部分读到一个单独的数组中
            for (i = currentIndex; i < currentIndex + 30; i++)
            {
                bytAlbum[j] = Info[i];
                position++;
                j++;
            }
            currentIndex = position;
            myMp3Info.Album = ByteToString(bytAlbum, Encoding);

            //获取年
            str = null;
            j = 0;
            byte[] bytYear = new byte[4]; //将年部分读到一个单独的数组中
            for (i = currentIndex; i < currentIndex + 4; i++)
            {
                bytYear[j] = Info[i];
                position++;
                j++;
            }
            currentIndex = position;
            myMp3Info.Year = ByteToString(bytYear, Encoding);

            //获取注释
            str = null;
            j = 0;
            byte[] bytComment = new byte[28]; //将注释部分读到一个单独的数组中
            for (i = currentIndex; i < currentIndex + 25; i++)
            {
                bytComment[j] = Info[i];
                position++;
                j++;
            }
            currentIndex = position;
            myMp3Info.Comment = ByteToString(bytComment, Encoding);

            //以下获取保留位
            myMp3Info.reserved1 = (char)Info[++position];
            myMp3Info.reserved2 = (char)Info[++position];
            myMp3Info.reserved3 = (char)Info[++position];

            //
            return myMp3Info;
        }
        #endregion

        /// <summary>
        /// 将字节数组转换成字符串
        /// </summary>
        /// <param name = "b">字节数组</param>
        /// <returns>返回转换后的字符串</returns>

        public static string ByteToString(byte[] SourceByte, System.Text.Encoding Encoding)
        {
            string str = Text.Encoding.Main.ByteToString(SourceByte, Encoding);

            //去掉无用字符
            str = str.Substring(0, str.IndexOf('/0') >= 0 ? str.IndexOf('/0') : str.Length);

            return str;
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值