【问题标题】:Get image from MP3 file with Java ID3 Tag library使用 Java ID3 标签库从 MP3 文件中获取图像
【发布时间】:2016-06-19 19:22:02
【问题描述】:

我正在尝试使用 Java ID3 Tag Library 从 mp3 文件中获取图像。我已经设法获取和编辑了大部分标签,但是在获取图像时遇到了一些问题。我已经了解到,图像存储在带有标题 APIC 的 ID3v2 标记中。我尝试以与其他标签类似的方式阅读它:

ID3v2_3 id3v2 = (ID3v2_3) mp3File.getID3v2Tag();
AbstractID3v2Frame frame = id3v2.getFrame("APIC");

if (frame != null) {
    FrameBodyAPIC frameBody = (FrameBodyAPIC)frame.getBody();
    // ...
}

问题是无论我在应用程序中加载什么 MP3 文件,APIC 框架都是NULL。我确信这首歌有一张图片,因为它在 Windows Media Player 中正确显示。有没有人使用Java ID3 Tag Library 库并设法加载歌曲的图片?如有任何帮助,我将不胜感激。


更新

我认为这可能是文件损坏的问题。因此,我尝试先设置一个图像框架,然后再尝试读取它。不幸的是,结果相同 - 框架始终为null。我展示了我使用的代码:

public void setImage(ID3v2_3 id3v2, byte[] image) {
    AbstractID3v2Frame frame = id3v2.getFrame("APIC");

    // frame is always null
    if (frame == null) {
        frame = new ID3v2_3Frame();
    }

    FrameBodyAPIC frameBody = (FrameBodyAPIC) frame.getBody();

    if (frameBody == null) {
        frameBody = new FrameBodyAPIC();
    }

    frameBody.setObject("Picture Data", image);
    frameBody.setObject("MIME Type", "image/png");
    frame.setBody(frameBody);

    // Set the newly created frame to mp3 file
    id3v2.setFrame(frame);

    // frame is still null - even just after setting it!
    frame = id3v2.getFrame("APIC");
}

我使用此方法更新其他标签并且它有效,但在这种特殊情况下它不是。有谁知道这可能是什么原因?

【问题讨论】:

    标签: java tags mp3 id3


    【解决方案1】:

    也许这段代码(未经测试,来源:https://sourceforge.net/p/javamusictag/discussion/253424/thread/4300f7b1/)可以提供帮助?

      187     private ImageData extractImageFromFile(String srcFile) throws Exception {
      188         ImageData imageData = null;
      189         File sourceFile = new File(srcFile);
      190         MP3File mp3file = new MP3File(sourceFile);
      191         FilenameTag fileNameTag = mp3file.getFilenameTag();
      192         AbstractID3v2 id3v2 = mp3file.getID3v2Tag();
      193         if (id3v2 != null) {
      194             AbstractID3v2Frame apic = id3v2.getFrame(PICTURE_TAG);
      195             if (apic != null) {
      196                 AbstractMP3FragmentBody apicBody = apic.getBody();
      197                 String mimeType = (String) apicBody.getObject(MIME_TAG);
      198                 String fileExtension = getFileExtensionFromMimeType(mimeType);
      199                 if (fileExtension == null)
      200                     fileExtension = "jpg";
      201                 if (fileExtension.charAt(0) == '.')
      202                     fileExtension = fileExtension.substring(1);
      203                 Object bytes = apicBody.getObject(PICTURE_DATA_TAG);
      204                 if (bytes != null) {
      205                     imageData = new ImageData();
      206                     imageData.bytes = (byte[]) bytes;
      207                     imageData.fileExtension = fileExtension;
      208                 }
      209             }
      210         }
      211         return imageData;
      212     }
    

    【讨论】:

    • 感谢您的回答。不幸的是,它并没有解决问题,因为无论我做什么,我都会得到null 而不是相框。在上面的代码中,它位于第 194 行:AbstractID3v2Frame apic = id3v2.getFrame(PICTURE_TAG);。因为我得到了null,所以其余的代码都没有执行。我已经更新了我的问题以更好地呈现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 2011-09-12
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多