本文实例讲述了c#实现获取mp3标签信息的方法。分享给大家供大家参考,具体如下:
使用系统; 使用System.Collections.Generic; 使用text; 使用先; 名称空间Foxer_Player_1._1 { 公共struct Mp3Info { 公共字符串识别;//标签,三个字节 公共字符串标题;//歌曲名,30个字节 公共字符串艺术家;//歌手名,30个字节 公共字符串专辑;//所属唱片,30个字节 公共字符串;//年,4个字符 公共字符串评论;//注释,28个字节 公共字符reserved1;//保留位,一个字节 公共字符reserved2;//保留位,一个字节 公共字符reserved3;//保留位,一个字节 }///& lt; summary>///Mp3文件信息类///& lt;/summary> 公开课Mp3FileInfo { Mp3Info信息;///& lt; summary>///构造函数、输入文件名即得到信息///& lt;/summary>///& lt;参数name=" mp3FilePos祝辞& lt;/param> 公共Mp3FileInfo(字符串mp3FilePos) { 信息=getMp3Info (getLast128 (mp3FilePos)); }///& lt; summary>///获取整理后的Mp3文件名,这里以标题和艺术家名定文件名///& lt;/summary>///& lt; returns> & lt;/returns> 公共字符串GetOriginalName () { 返回formatString (info.Title.Trim ()) +“-”+ formatString (info.Artist.Trim ()); }///& lt; summary>///去除\ 0字符///& lt;/summary>///& lt;参数name=" str祝辞& lt;/param>///& lt; returns> & lt;/returns> 私有静态字符串formatString (String str) { 返回str.Replace (" \ 0 ", " "); }///& lt; summary>///获取MP3文件最后128个字节///& lt;/summary>///& lt;参数name="文件名"祝辞文件名& lt;/param>///& lt; returns>返回字节数组& lt;/returns> 公共静态byte [] getLast128(字符串文件名) { fs=new FileStream文件流(文件名,FileMode。开放、Fileaccess.Read); 流流=f; 流。SeekOrigin.End寻求(-128); const int seekPos=128; int rl=0; byte[]信息=新字节(seekPos); rl=流。读(信息0 seekPos); fs.Close (); stream.Close (); 返回信息; }///& lt; summary>///获取MP3歌曲的相关信息///& lt;/summary>///& lt;参数name="信息"祝辞从MP3文件中截取的二进制信息& lt;/param>///& lt; returns>返回一个Mp3Info结构& lt;/returns> 公共静态Mp3Info getMp3Info (byte[]信息) { Mp3Info Mp3Info=new Mp3Info (); 字符串str=零; int我; int位置=0;//循环的起始值 int currentIndex=0;//信息的当前索引值//获取标签标识 (我=currentIndex;我& lt;currentIndex + 3;我+ +) { str=str + (char)信息(我); 位置+ +; } currentIndex=位置; mp3Info。识别=str;//获取歌名 str=零; byte [] bytTitle=新字节[30];//将歌名部分读到一个单独的数组中 int j=0; (我=currentIndex;我& lt;currentIndex + 30;我+ +) { bytTitle [j]=[我]信息; 位置+ +; + +; } currentIndex=位置; mp3Info。Title=Foxer_Player_1._1.Mp3FileInfo.byteToString (bytTitle);//获取歌手名 str=零; j=0; byte [] bytArtist=新字节[30];//将歌手名部分读到一个单独的数组中 (我=currentIndex;我& lt;currentIndex + 30;我+ +) { bytArtist [j]=[我]信息; 位置+ +; + +; } currentIndex=位置; mp3Info。艺术家=Foxer_Player_1._1.Mp3FileInfo.byteToString (bytArtist);//获取唱片名 str=零; j=0; byte [] bytAlbum=新字节[30];//将唱片名部分读到一个单独的数组中 (我=currentIndex;我& lt;currentIndex + 30;我+ +) { bytAlbum [j]=[我]信息; 位置+ +; + +; } currentIndex=位置; mp3Info。相册=Foxer_Player_1._1.Mp3FileInfo.byteToString (bytAlbum);//获取年 str=零; j=0; byte [] bytYear=新字节[4];//将年部分读到一个单独的数组中 (我=currentIndex;我& lt;currentIndex + 4;我+ +) { bytYear [j]=[我]信息; 位置+ +; + +; } currentIndex=位置; mp3Info。年=Foxer_Player_1._1.Mp3FileInfo.byteToString (bytYear);//获取注释 str=零; j=0; byte [] bytComment=新字节[28];//将注释部分读到一个单独的数组中 (我=currentIndex;我& lt;currentIndex + 25;我+ +) { bytComment [j]=[我]信息; 位置+ +; + +; } currentIndex=位置; mp3Info。评论=Foxer_Player_1._1.Mp3FileInfo.byteToString (bytComment);//以下获取保留位 mp3Info。reserved1=(char)信息(+ +位置); mp3Info。reserved2=(char)信息(+ +位置); mp3Info。reserved3=(char)信息(+ +位置); 返回mp3Info; }///& lt; summary>///将字节数组转换成字符串///& lt;/summary>///& lt;参数name=" b "祝辞字节数组& lt;/param>///& lt; returns>返回转换后的字符串& lt;/returns> 公共静态字符串byteToString (byte [] b) { 编码enc=Encoding.GetEncoding (GB2312”); 字符串str=enc.GetString (b); str=str.Substring (0, str.IndexOf(“# #内容”)在=0 & # 63;str.IndexOf(“# #”内容):str.Length);//去掉无用字符 返回str; } } }c#实现获取mp3标签信息的方法