WPF C#,从音频文件中获取信息
嘿!
我想从 C# (wpf) 中的音频文件(标题、艺术家等)获取一些信息。 MediaElement 不提供此选项,因此我使用了此代码(直接读取字节):
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;
}
此代码的问题是,有时它不会给出完整的标题或仅代表小立方体。 (如果我在MeidaPlayer中打开媒体,我可以看到完整的标题)
我不确定 GetString(byte[],int,int) 的参数,也许我在那里犯了错误。
在我的程序中:
在媒体播放器中:
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 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在阅读 ID3v1 标题,该标题将标题限制为 30 个字符。此外,任何比这短的内容都会用空格或零填充,后者会转换为您看到的框。您需要使用以下内容删除这些内容:
媒体播放器可能正在读取 ID3v1 扩展标签,该标签位于您正在读取的标题之前。请参阅上面的链接了解更多信息。但它实际上是您正在读取的 128 字节之前的 227 字节。
在扩展标题中,标题(和其他)的长度限制为 60 个字符,而不是 30 个。
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:
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.