利用C#显示MP3的标签信息
2024-07-21 02:19:04
供稿:网友
//目的:列出mp3的一些标签信息
//作者:李艳庆
using system;
using system.io;
namespace mp3infons
{
class mp3info
{
struct mp3infostruct
{
public string mp3flag;
public string title;
public string singer;
public string album;
public string year;
public string comment;
}
private string mp3file;
mp3infostruct mp3struct;
public mp3info(string mp3name)
{
mp3file = mp3name;
mp3struct = new mp3infostruct();
}
public bool readmp3info()
{
bool isset=false;
byte[] b = new byte[128];
try
{
filestream fs = new filestream(mp3file, filemode.open);
fs.seek(-128, seekorigin.end);
fs.read(b, 0, 128);
mp3struct.mp3flag = system.text.encoding.default.getstring(b, 0, 3);
if (mp3struct.mp3flag.compareto("tag")==0)
{
//检查是否设置了标签
isset=true;
mp3struct.title = system.text.encoding.default.getstring (b,3,30);
mp3struct.singer = system.text.encoding.default.getstring (b,33,30);
mp3struct.album = system.text.encoding.default.getstring (b,63,30);
mp3struct.year = system.text.encoding.default.getstring (b,93,4);
mp3struct.comment = system.text.encoding.default.getstring (b,97,30);
}
fs.close();
}
catch(exception e)
{
system.console.writeline(e.message);
}
return isset;
}
public void printmp3info()
{
system.console.writeline("mp3附加信息:");
system.console.writeline("-----------------------------");
system.console.writeline("标 题: " + mp3struct.title);
system.console.writeline("歌 手: " + mp3struct.singer);
system.console.writeline("唱片集: " + mp3struct.album);
system.console.writeline("出版期: " + mp3struct.year);
system.console.writeline("备 注: " + mp3struct.comment);
}
}
public class mainmp3
{
static void main(string[] args)
{
if (args.length == 1)
{
mp3info mp3 = new mp3info(args[0]);
bool f = mp3.readmp3info();
if (f)
{
mp3.printmp3info();
}
else
{
system.console.writeline("该mp3没有标注");
}
}
else
{
system.console.writeline("参数不正确,只能跟一个参数");
}
}
}
}