【问题标题】:Program not finding any ID3 info from an MP3 file even though the specified file has ID3 info即使指定的文件具有 ID3 信息,程序也无法从 MP3 文件中找到任何 ID3 信息
【发布时间】:2020-11-06 13:09:14
【问题描述】:

我尝试指定文件的直接路由,我尝试将路由作为 File 变量的值,但无济于事。该程序识别文件(因为它不输出错误消息),但它不将其识别为具有它确实具有的 ID3 信息的 MP3 文件。代码如下:

import java.io.*;

public class ID3Reader {
    public static void main(String[] arguments) {
        File song = new File(arguments[0]); 
        try (FileInputStream file = new FileInputStream(song)) {
            int size = (int) song.length();
            file.skip(size - 128);
            byte[] last128 = new byte[128];
            file.read(last128);
            String id3 = new String(last128);
            String tag = id3.substring(0, 3);
            if (tag.equals("TAG")) {
                System.out.println("Title: " + id3.substring(3, 32));
                System.out.println("Artist: " + id3.substring(33, 62));
                System.out.println("Album: " + id3.substring(63, 91));
                System.out.println("Year: " + id3.substring(93, 97));
            } else {
                System.out.println(arguments[0] + " does not contain"
                     + " ID3 info.");
            }
            file.close();
        } catch (IOException ioe) {
            System.out.println("Error -- " + ioe.toString());
        }
    }
}

//Output is "C:\Users\gabbs\OneDrive\Music\4 Non Blondes - What's Up.mp3 does not contain ID3 info."


【问题讨论】:

  • 请记住,ID3v1 和 ID3v2 是完全不同的标准。您的 ID3 阅读器可能只做其中之一。
  • “不包含”...打印tag 包含的内容怎么样,这样您就知道实际存在什么?也许你已经偏离了 1 个字节。

标签: java file stream mp3 id3


【解决方案1】:

尝试打印出字符串 id3。可能只是缺少 CRLF 左右。当您看到 id3 的样子时,您可以对其进行逆向工程。

我运行了您的程序,但在读取文件时遇到了一些问题。我将其修复如下:

public static void main(String[] args) {
    ClassLoader classLoader = new ID3Reader().getClass().getClassLoader();

    File song = new File(classLoader.getResource(args[0]).getFile());
    try (FileInputStream file = new FileInputStream(song)) {
        int size = (int) song.length();
        file.skip(size - 128);
        byte[] last128 = new byte[128];
        file.read(last128);
        String id3 = new String(last128);
        String tag = id3.substring(0, 3);
        System.out.println(id3);
        if (tag.equals("TAG")) {
            System.out.println("Title: " + id3.substring(3, 32));
            System.out.println("Artist: " + id3.substring(33, 62));
            System.out.println("Album: " + id3.substring(63, 91));
            System.out.println("Year: " + id3.substring(93, 97));
        } else {
            System.out.println(args[0] + " does not contain"
                    + " ID3 info.");
        }
    } catch (IOException ioe) {
        System.out.println("Error -- " + ioe.toString());
    }
}

【讨论】:

  • 它打印了 128 个 U
  • 请在您的代码中使用 cmets 并避免像“一些问题”这样的泛型,而是制定详细信息。没有人知道你解决了哪个问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 2012-03-29
相关资源
最近更新 更多