Hy!
I'd like to get some informations from an audio file (title, artist, etc.) in C# (wpf). The MediaElement doesn't provides this option, so I used this code (read bytes directly):public string[] GetAudioFileInfo(string path)
{
path = Uri.UnescapeDataString(path);
byte[] b = new byte[128];
string[] infos = new string[5]; //Title; Singer; Album; Year; Comm;
bool isSet = false;
//Read bytes
try
{
FileStream fs = new FileStream(path, FileMode.Open);
fs.Seek(-128, SeekOrigin.End);
fs.Read(b, 0, 128);
//Set flag
String sFlag = System.Text.Encoding.Default.GetString(b, 0, 3);
if (sFlag.CompareTo("TAG") == 0) isSet = true;
if (isSet)
{
infos[0] = System.Text.Encoding.Default.GetString(b, 3, 30); //Title
infos[1] = System.Text.Encoding.Default.GetString(b, 33, 30); //Singer
infos[2] = System.Text.Encoding.Default.GetString(b, 63, 30); //Album
infos[3] = System.Text.Encoding.Default.GetString(b, 93, 4); //Year
infos[4] = System.Text.Encoding.Default.GetString(b, 97, 30); //Comm
}
fs.Close();
fs.Dispose();
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
return infos;
}
The problem with this code, that sometimes it doesn't gives the full title or represents only little cubes. (If I开发者_JAVA技巧 open the media in MeidaPlayer, than I can see the full title)
I'm not sure the parameters of the GetString(byte[],int,int), maybe I make mistakes there.In my prgram:
In media player:You are reading the ID3v1 header, which limits the title to 30 characters. In addition, anything shorter than that is padded with spaces or zeros, the latter of which translates into the boxes you see. You'd need to strip those off using something like:
myString = myString.Replace("\0", "")
Chances are the media player is reading the ID3v1 extended tag, which is placed before the header you are reading. See the link above for more information. But it's effectively the 227 bytes before the 128 bytes you are reading.
In the extended header, the title (and others) is limited to 60 characters, instead of 30.
精彩评论