SoFunction
Updated on 2025-03-03

C# implements the method of obtaining mp3 tag information

This article describes the method of C# to obtain mp3 tag information. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
namespace Foxer_Player_1._1
{
  public struct Mp3Info
  {
    public string identify;   //TAG, three bytes    public string Title;    //Song title, 30 bytes    public string Artist;    //Singer name, 30 bytes    public string Album;    //The record, 30 bytes    public string Year;     //Year, 4 characters    public string Comment;   // Comment, 28 bytes    public char reserved1;   //Reserve bit, one byte    public char reserved2;   //Reserve bit, one byte    public char reserved3;   //Reserve bit, one byte  }
  /// <summary>
  /// Mp3 file information class  /// </summary>
  public class Mp3FileInfo
  {
    Mp3Info info;
    /// <summary>
    /// Constructor, enter the file name and get information    /// </summary>
    /// <param name="mp3FilePos"></param>
    public Mp3FileInfo(String mp3FilePos)
    {
      info = getMp3Info(getLast128(mp3FilePos));
    }
    /// <summary>
    /// Get the sorted Mp3 file name, here is the title and artist name to determine the file name    /// </summary>
    /// <returns></returns>
    public String GetOriginalName()
    {
      return formatString(()) + "-" + formatString(());
    }
    /// <summary>
    /// Remove \0 characters    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private static String formatString(String str)
    {
      return ("\0", "");
    }
    /// <summary>
    /// Get the last 128 bytes of the MP3 file    /// </summary>
    /// <param name="FileName">File name</param>    /// <returns>Return byte array</returns>    public static byte[] getLast128(string FileName)
    {
      FileStream fs = new FileStream(FileName, , );
      Stream stream = fs;
      (-128, );
      const int seekPos = 128;
      int rl = 0;
      byte[] Info = new byte[seekPos];
      rl = (Info, 0, seekPos);
      ();
      ();
      return Info;
    }
    /// &lt;summary&gt;
    /// Get relevant information about MP3 songs    /// &lt;/summary&gt;
    /// <param name = "Info">Binary information intercepted from MP3 files</param>    /// <returns>Returns a Mp3Info structure</returns>    public static Mp3Info getMp3Info(byte[] Info)
    {
      Mp3Info mp3Info = new Mp3Info();
      string str = null;
      int i;
      int position = 0;//The start value of the loop      int currentIndex = 0;//The current index value of Info      //Get TAG ID      for (i = currentIndex; i &lt; currentIndex + 3; i++)
      {
        str = str + (char)Info[i];
        position++;
      }
      currentIndex = position;
       = str;
      //Get the song title      str = null;
      byte[] bytTitle = new byte[30];//Read the song title part into a separate array      int j = 0;
      for (i = currentIndex; i &lt; currentIndex + 30; i++)
      {
        bytTitle[j] = Info[i];
        position++;
        j++;
      }
      currentIndex = position;
       = Foxer_Player_1._1.(bytTitle);
      //Get the singer's name      str = null;
      j = 0;
      byte[] bytArtist = new byte[30];//Read the singer's name part into a separate array      for (i = currentIndex; i &lt; currentIndex + 30; i++)
      {
        bytArtist[j] = Info[i];
        position++;
        j++;
      }
      currentIndex = position;
       = Foxer_Player_1._1.(bytArtist);
      //Get the record name      str = null;
      j = 0;
      byte[] bytAlbum = new byte[30];//Read the record title part into a separate array      for (i = currentIndex; i &lt; currentIndex + 30; i++)
      {
        bytAlbum[j] = Info[i];
        position++;
        j++;
      }
      currentIndex = position;
       = Foxer_Player_1._1.(bytAlbum);
      //Get year      str = null;
      j = 0;
      byte[] bytYear = new byte[4];//Read the year part into a separate array      for (i = currentIndex; i &lt; currentIndex + 4; i++)
      {
        bytYear[j] = Info[i];
        position++;
        j++;
      }
      currentIndex = position;
       = Foxer_Player_1._1.(bytYear);
      //Get comment      str = null;
      j = 0;
      byte[] bytComment = new byte[28];//Read the comment part into a separate array      for (i = currentIndex; i &lt; currentIndex + 25; i++)
      {
        bytComment[j] = Info[i];
        position++;
        j++;
      }
      currentIndex = position;
       = Foxer_Player_1._1.(bytComment);
      //The following gets the reserved bit      mp3Info.reserved1 = (char)Info[++position];
      mp3Info.reserved2 = (char)Info[++position];
      mp3Info.reserved3 = (char)Info[++position];
      return mp3Info;
    }
    /// &lt;summary&gt;
    /// Convert byte array to string    /// &lt;/summary&gt;
    /// <param name = "b">Byte array</param>    /// <returns>Returns converted string</returns>    public static string byteToString(byte[] b)
    {
      Encoding enc = ("GB2312");
      string str = (b);
      str = (0, ("#CONTENT#") >= 0 ? ("#CONTENT#") : );// Remove useless characters      return str;
    }
  }
}

For more information about C# related content, please check out the topic of this site:Summary of common techniques for C# file operation》、《Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.