WPF C#,从音频文件中获取信息

发布于 2024-11-03 07:10:04 字数 1633 浏览 0 评论 0原文

嘿!
我想从 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) 的参数,也许我在那里犯了错误。

在我的程序中: This is the result
在媒体播放器中: 在此处输入图像描述

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: This is the result
In media player: enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2024-11-10 07:10:04

您正在阅读 ID3v1 标题,该标题将标题限制为 30 个字符。此外,任何比这短的内容都会用空格或零填充,后者会转换为您看到的框。您需要使用以下内容删除这些内容:

myString = myString.Replace("\0", "")

媒体播放器可能正在读取 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文