练习一下C#
参考了网上的一些资料,写了一个类
ID3v1还是比较简单的,也够用了
通过这个也熟悉了读取文件,特别是二进制的文件的方法,还有目录遍历的方法。
下面把源文件贴出来
ID3v1.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace tigerlihao
{
public struct ID3v1tag
{
public string header; //标签头
public string title; //标题
public string artist; //艺人
public string album; //专辑
public string year; //年代
public string comment; //备注
public int trackNumberFlag;//编号标记
public int trackNumber; //编号
public int genre; //类型
}
public class ID3v1
{
private string mp3File;
public ID3v1tag mp3Tag;
public ID3v1(string mp3FileName)
{
mp3File = mp3FileName;
mp3Tag = new ID3v1tag();
}
public bool getTagInfo()
{
bool hasTag = false;
byte[] b = new byte[128];
FileStream fs = new FileStream(mp3File, FileMode.Open);
fs.Seek(-128, SeekOrigin.End);
fs.Read(b, 0, 128);
mp3Tag.header = Encoding.Default.GetString(b, 0, 3);
if (mp3Tag.header.CompareTo("TAG") == 0)
{
hasTag = true;
mp3Tag.title = Encoding.Default.GetString(b, 3, 30);
mp3Tag.artist = Encoding.Default.GetString(b, 33, 30);
mp3Tag.album = Encoding.Default.GetString(b, 63, 30);
mp3Tag.year = Encoding.Default.GetString(b, 93, 4);
mp3Tag.trackNumberFlag = b[125];
if (mp3Tag.trackNumberFlag == 0)
{
mp3Tag.comment = Encoding.Default.GetString(b, 97, 28);
mp3Tag.trackNumber = b[126];
}
else
{
mp3Tag.comment = Encoding.Default.GetString(b, 97, 30);
mp3Tag.trackNumber = 0;
}
mp3Tag.genre = b[127];
}
fs.Close();
return hasTag;
}
public override string ToString()
{
string str="";
string[] strGenre =
{
"Blues",
"ClassicRock",
"Country",
"Dance",
"Disco",
"Funk",
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"NewAge",
"Oldies",
"Other",
"Pop",
"R&B",
"Rap",
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative",
"Ska",
"DeathMetal",
"Pranks",
"Soundtrack",
"Euro-Techno",
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion",
"Trance",
"Classical",
"Instrumental",
"Acid",
"House",
"Game",
"SoundClip",
"Gospel",
"Noise",
"AlternRock",
"Bass",
"Soul",
"Punk",
"Space",
"Meditative",
"InstrumentalPop",
"InstrumentalRock",
"Ethnic",
"Gothic",
"Darkwave",
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream",
"SouthernRock",
"Comedy",
"Cult",
"Gangsta",
"Top40",
"ChristianRap",
"Pop/Funk",
"Jungle",
"NativeAmerican",
"Cabaret",
"NewWave",
"Psychadelic",
"Rave",
"Showtunes",
"Trailer",
"Lo-Fi",
"Tribal",
"AcidPunk",
"AcidJazz",
"Polka",
"Retro",
"Musical",
"Rock&Roll",
"HardRock",
/* Extended genres */
"Folk",
"Folk-Rock",
"NationalFolk",
"Swing",
"FastFusion",
"Bebob",
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde",
"GothicRock",
"ProgessiveRock",
"PsychedelicRock",
"SymphonicRock",
"SlowRock",
"BigBand",
"Chorus",
"EasyListening",
"Acoustic",
"Humour",
"Speech",
"Chanson",
"Opera",
"ChamberMusic",
"Sonata",
"Symphony",
"BootyBass",
"Primus",
"PornGroove",
"Satire",
"SlowJam",
"Club",
"Tango",
"Samba",
"Folklore",
"Ballad",
"PowerBallad",
"RhythmicSoul",
"Freestyle",
"Duet",
"PunkRock",
"DrumSolo",
"Acapella",
"Euro-House",
"DanceHall",
"Goa",
"Drum&Bass",
"Club-House",
"Hardcore",
"Terror",
"Indie",
"BritPop",
"Negerpunk",
"PolskPunk",
"Beat",
"ChristianGangstaRap",
"HeavyMetal",
"BlackMetal",
"Crossover",
"ContemporaryChristian",
"ChristianRock",
"Merengue",
"Salsa",
"TrashMetal",
"Anime",
"JPop",
"Synthpop"
};
str += "音轨:" + mp3Tag.trackNumber.ToString() + "\n";
str += "标题:" + mp3Tag.title + "\n";
str += "艺人:" + mp3Tag.artist + "\n";
str += "专辑:" + mp3Tag.album + "\n";
str += "年代:" + mp3Tag.year + "\n";
if (mp3Tag.genre>147)
str += "类别:N/A\n";
else
str += "类别:" + strGenre[mp3Tag.genre] + "\n";
str += "备注:" + mp3Tag.comment + "\n";
return str;
}
}
}
Program.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using tigerlihao;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
FileInfo[] mp3Files;
DirectoryInfo mp3Dir = new DirectoryInfo(@"D:\My Documents\My Music");
mp3Files = mp3Dir.GetFiles("*.mp3", SearchOption.TopDirectoryOnly);
ID3v1 tag;
for (int i = 0; i < mp3Files.Length; i++)
{
Console.Write(mp3Files[i].FullName+"\n");
tag = new ID3v1(mp3Files[i].FullName);
bool hasTag = tag.getTagInfo();
if (hasTag)
Console.Write(tag.ToString());
tag = null;
Console.Write("+++++++++++++++++++++++++++++++++\n");
}
}
}
}
有ID3v2的么