java - 即使指定的文件具有 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."
解决方案
尝试打印出字符串 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());
}
}
推荐阅读
- swift - Swift MessageKit 问题
- python - Pycharm 不会将我的函数返回到控制台
- javascript - 尝试使用一些 trig 和 JavaScript 画布重新创建大逃杀红区功能
- entity-framework - ASP.NET core 2.1 数据库迁移
- excel - 如何找到等于给定值的对应元素的最小数字总和?
- reactjs - 将 gql 函数转换为插入变量的字符串
- vue.js - Vuex如何跟踪索引并将其传递给组件?
- vue.js - 带有 Vuex 的 vue-table-2 - 无法更改计数属性
- perl - 在 Perl 中读取文件会中断编码
- react-native - React Native Linking - 如何正确获取网址?