欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现获取mp3 Tag信息的方法

程序员文章站 2023-12-01 11:50:52
本文实例讲述了c#实现获取mp3 tag信息的方法。分享给大家供大家参考,具体如下: using system; using system.collection...

本文实例讲述了c#实现获取mp3 tag信息的方法。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.text;
using system.io;
namespace foxer_player_1._1
{
  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;   //保留位,一个字节
  }
  /// <summary>
  /// mp3文件信息类
  /// </summary>
  public class mp3fileinfo
  {
    mp3info info;
    /// <summary>
    /// 构造函数,输入文件名即得到信息
    /// </summary>
    /// <param name="mp3filepos"></param>
    public mp3fileinfo(string mp3filepos)
    {
      info = getmp3info(getlast128(mp3filepos));
    }
    /// <summary>
    /// 获取整理后的mp3文件名,这里以标题和艺术家名定文件名
    /// </summary>
    /// <returns></returns>
    public string getoriginalname()
    {
      return formatstring(info.title.trim()) + "-" + formatstring(info.artist.trim());
    }
    /// <summary>
    /// 去除\0字符
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private static string formatstring(string str)
    {
      return str.replace("\0", "");
    }
    /// <summary>
    /// 获取mp3文件最后128个字节
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <returns>返回字节数组</returns>
    public static byte[] getlast128(string filename)
    {
      filestream fs = new filestream(filename, filemode.open, fileaccess.read);
      stream stream = fs;
      stream.seek(-128, seekorigin.end);
      const int seekpos = 128;
      int rl = 0;
      byte[] info = new byte[seekpos];
      rl = stream.read(info, 0, seekpos);
      fs.close();
      stream.close();
      return info;
    }
    /// <summary>
    /// 获取mp3歌曲的相关信息
    /// </summary>
    /// <param name = "info">从mp3文件中截取的二进制信息</param>
    /// <returns>返回一个mp3info结构</returns>
    public static mp3info getmp3info(byte[] info)
    {
      mp3info mp3info = 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;
      mp3info.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;
      mp3info.title = foxer_player_1._1.mp3fileinfo.bytetostring(byttitle);
      //获取歌手名
      str = null;
      j = 0;
      byte[] bytartist = new byte[30];//将歌手名部分读到一个单独的数组中
      for (i = currentindex; i < currentindex + 30; i++)
      {
        bytartist[j] = info[i];
        position++;
        j++;
      }
      currentindex = position;
      mp3info.artist = foxer_player_1._1.mp3fileinfo.bytetostring(bytartist);
      //获取唱片名
      str = null;
      j = 0;
      byte[] bytalbum = new byte[30];//将唱片名部分读到一个单独的数组中
      for (i = currentindex; i < currentindex + 30; i++)
      {
        bytalbum[j] = info[i];
        position++;
        j++;
      }
      currentindex = position;
      mp3info.album = foxer_player_1._1.mp3fileinfo.bytetostring(bytalbum);
      //获取年
      str = null;
      j = 0;
      byte[] bytyear = new byte[4];//将年部分读到一个单独的数组中
      for (i = currentindex; i < currentindex + 4; i++)
      {
        bytyear[j] = info[i];
        position++;
        j++;
      }
      currentindex = position;
      mp3info.year = foxer_player_1._1.mp3fileinfo.bytetostring(bytyear);
      //获取注释
      str = null;
      j = 0;
      byte[] bytcomment = new byte[28];//将注释部分读到一个单独的数组中
      for (i = currentindex; i < currentindex + 25; i++)
      {
        bytcomment[j] = info[i];
        position++;
        j++;
      }
      currentindex = position;
      mp3info.comment = foxer_player_1._1.mp3fileinfo.bytetostring(bytcomment);
      //以下获取保留位
      mp3info.reserved1 = (char)info[++position];
      mp3info.reserved2 = (char)info[++position];
      mp3info.reserved3 = (char)info[++position];
      return mp3info;
    }
    /// <summary>
    /// 将字节数组转换成字符串
    /// </summary>
    /// <param name = "b">字节数组</param>
    /// <returns>返回转换后的字符串</returns>
    public static string bytetostring(byte[] b)
    {
      encoding enc = encoding.getencoding("gb2312");
      string str = enc.getstring(b);
      str = str.substring(0, str.indexof("#content#") >= 0 ? str.indexof("#content#") : str.length);//去掉无用字符
      return str;
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#文件操作常用技巧汇总》、《c#遍历算法与技巧总结》、《c#程序设计之线程使用技巧总结》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。