0

我正在尝试使用 jaudiotagger 更改音频文件的专辑封面,但它不起作用。关于这个主题有很多问题,但没有一个能满足我的要求。其他字段,如艺术家、音频名称、专辑等已成功更新,但专辑插图保持不变。

我用来更改艺术品的代码:

  public void changeAlbumArt(Uri data) {


    Artwork cover = null;
    try {
        AudioFile f = null;
        try {
            f = AudioFileIO.read(new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData()));
        } catch (CannotReadException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TagException e) {
            e.printStackTrace();
        } catch (ReadOnlyFileException e) {
            e.printStackTrace();
        } catch (InvalidAudioFrameException e) {
            e.printStackTrace();
        }
        Tag tag = null;
        if (f != null) {
            tag = f.getTag();
        }


        try {
            if(tag!=null) {
                cover = ArtworkFactory.createArtworkFromFile(new File(data.getPath()));
                tag.deleteArtworkField();
                tag.setField(cover);

            }
        } catch (FieldDataInvalidException e) {
            e.printStackTrace();
        }
        try {
            if (f != null) {
                f.commit();
            }
        } catch (CannotWriteException e) {
            e.printStackTrace();
        }


    } catch (Exception e) {
        e.printStackTrace();
    }

    File file = new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData());
    Uri uri = Uri.fromFile(file);
    Intent scanFileIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
    sendBroadcast(scanFileIntent);


}
4

1 回答 1

0

我曾经有过这个工作,但由于 jaudiotagger 需要一些 io 文件,而这些文件在 android 中不可用,所以我已经恢复到 jid3。此外,android Gingerbread+ 写入外部存储需要 SAF 框架。我已经设法修改了 jid3 库,所以它可以工作,但只适用于 mp3。

对于 mp3 文件:

     public String setMp3AlbumArt(File SourceFile, byte[] bytes)
        throws Exception {
    String error = null;
    try {
        MP3File musicFile = (MP3File) AudioFileIO.read(SourceFile);
        AbstractID3v2Tag tag = musicFile.getID3v2Tag();
        if (tag != null) {
            Artwork artwork = null;
            try {
                artwork = ArtworkFactory.createArtworkFromFile(SourceFile);
            } catch (IOException e) {
                e.printStackTrace();
                error = e.getMessage();
            }
            if (artwork != null) {
                artwork.setBinaryData(bytes);
                tag.deleteArtworkField();
                tag.setField(artwork);
                musicFile.setTag(tag);
                musicFile.commit();
            } else {
                artwork.setBinaryData(bytes);
                tag.addField(artwork);
                tag.setField(artwork);
                musicFile.setTag(tag);
                musicFile.commit();
            }
        }

    } catch (CannotReadException | IOException | TagException
            | ReadOnlyFileException | InvalidAudioFrameException e) {
        error = e.getMessage();
    }
    return error;
}
于 2018-03-25T11:11:07.503 回答