首页 > 解决方案 > 即使指定的文件具有 ID3 信息,程序也无法从 MP3 文件中找到任何 ID3 信息

问题描述

我尝试指定文件的直接路由,我尝试将路由作为 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."


标签: javafilestreammp3id3

解决方案


尝试打印出字符串 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());
    }
}

推荐阅读