C # Read media information

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Namespace foxer_player_1. _ 1
{
Public struct mp3info
{
Public String identify; // tag, three bytes
Public String title; // song name, 30 bytes
Public String artist; // artist name, 30 bytes
Public String album; // The album name, 30 bytes.
Public String year; // year, 4 characters long
Public String comment; // comment, 28 bytes
Public char reserved1; // Reserved Bit, one byte
Public char reserved2; // Reserved Bit, one byte
Public char reserved3; // reserved byte
}
/// <Summary>
/// MP3 file information
/// </Summary>
Public class mp3fileinfo
{
Mp3info Info;
/// <Summary>
/// Constructor. Enter the file name to obtain the information.
/// </Summary>
/// <Param name = "mp3filepos"> </param>
Public mp3fileinfo (string mp3filepos)
{
Info = getmp3info (getlast128 (mp3filepos ));
}
/// <Summary>
/// Obtain the sorted MP3 file name. The file name is determined by the title and artist name.
/// </Summary>
/// <Returns> </returns>
Public String getoriginalname ()
{
Return formatstring (info. Title. Trim () + "-" + formatstring (info. Artist. Trim ());
}
/// <Summary>
/// Remove \ 0 characters
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Private Static string formatstring (string Str)
{
Return Str. Replace ("\ 0 ","");
}
/// <Summary>
/// Obtain the last 128 bytes of the MP3 file
/// </Summary>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> Returns a byte array </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>
/// Obtain information about mp3 songs
/// </Summary>
/// <Param name = "info"> binary information captured from an MP3 file </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 starting value of the loop
Int currentindex = 0; // The current index value of info
// Obtain the tag ID
For (I = currentindex; I <currentindex + 3; I ++)
{
STR = STR + (char) info [I];
Position ++;
}
Currentindex = position;
Mp3info. Identify = STR;

// Obtain the song name
STR = NULL;
Byte [] byttitle = new byte [30]; // read the song name to a separate array.
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 );

// Obtain the artist name
STR = NULL;
J = 0;
Byte [] bytartist = new byte [30]; // read the singer's name in a separate array.
For (I = currentindex; I <currentindex + 30; I ++)
{
Bytartist [J] = info [I];
Position ++;
J ++;
}
Currentindex = position;
Mp3info. Artist = foxer_player_1. _ 1. mp3fileinfo. bytetostring (bytartist );

// Obtain the recording name
STR = NULL;
J = 0;
Byte [] bytalbum = new byte [30]; // read the recording name to a separate array.
For (I = currentindex; I <currentindex + 30; I ++)
{
Bytalbum [J] = info [I];
Position ++;
J ++;
}
Currentindex = position;
Mp3info. album = foxer_player_1. _ 1. mp3fileinfo. bytetostring (bytalbum );

// Obtain the year
STR = NULL;
J = 0;
Byte [] bytyear = new byte [4]; // read the year to a separate array.
For (I = currentindex; I <currentindex + 4; I ++)
{
Bytyear [J] = info [I];
Position ++;
J ++;
}
Currentindex = position;
Mp3info. Year = foxer_player_1. _ 1. mp3fileinfo. bytetostring (bytyear );
// Obtain comments
STR = NULL;
J = 0;
Byte [] bytcomment = new byte [28]; // read the comments to a separate array.
For (I = currentindex; I <currentindex + 25; I ++)
{
Bytcomment [J] = info [I];
Position ++;
J ++;
}
Currentindex = position;
Mp3info. Comment = foxer_player_1. _ 1. mp3fileinfo. bytetostring (bytcomment );

// Obtain the reserved bits
Mp3info. reserved1 = (char) info [++ position];
Mp3info. reserved2 = (char) info [++ position];
Mp3info. reserved3 = (char) info [++ position];
Return mp3info;
}
/// <Summary>
/// Convert the byte array into a string
/// </Summary>
/// <Param name = "B"> byte array </param>
/// <Returns> returns the converted string </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); // Remove useless characters
Return STR;
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.