我正在使用我的 HTTP 服务器,我目前正在实现向客户端读取和显示文件(如 mp4、m4a、wav 等)的媒体标签和信息的能力。到目前为止,我的标签,如标题、曲目编号、年份、专辑、艺术家、版权等。使用JAudioTagger(此处提供二进制文件,此处提供网站)完美适用于多个文件扩展名。
我现在要做的是实现读取和转换图像数据或专辑插图/封面数据的能力,并将该数据分别以 png、jpeg 等格式发送给客户端。我已经访问并阅读了有关 APIC 标签的官方部分,但我无法弄清楚如何转换数据或数据在标签中实际开始的位置。
这是我编写的代码,用于从包含它的文件中检索专辑插图数据:
public static final byte[] readFileArtwork(File file) {
if(file == null || !file.isFile()) {
return null;
}
AudioFile afile = null;
try {
afile = AudioFileIO.read(file);
} catch(CannotReadException e) {
System.err.print("Unable to read file: ");
e.printStackTrace();
} catch(IOException e) {
System.err.print("An I/O Exception occurred: ");
e.printStackTrace();
} catch(TagException e) {
System.err.print("Unable to read file's tag data: ");
e.printStackTrace();
} catch(ReadOnlyFileException e) {//???
System.err.print("Unable to read file: File is read only: ");
e.printStackTrace();
} catch(InvalidAudioFrameException e) {
System.err.print("Unable to read file's audio frame data: ");
e.printStackTrace();
}
byte[] data = new byte[0];
if(afile == null) {
return data;
}
Iterator<TagField> tags = afile.getTag().getFields();
while(tags.hasNext()) {
TagField tag = tags.next();
if(tag.isBinary()) {
if(tag.getId().equals("APIC")) {
try {
data = tag.getRawContent();
} catch(UnsupportedEncodingException e) {
System.err.print("Unable to read file's image data: ");
e.printStackTrace();
}
}
}
}
return data == null ? new byte[0] : data;
}