Jaudiotagger ID3 TAG for android - can set artwork

2020-06-28 03:16发布

I'm relatively new to Java programming. Here i'm trying to use Jaudiotagger library for altering/creating new ID3 tag for mp3s without one in one of my android project. However, I really cannot set to made the library work correctly. That is, For mp3s with some ID3 tag already set, I can successfully write and change the metadata without any problems. But for those mp3s who has no ID3 tag set (blank), I can only manage to set the album cover artwork and other fields like artist, title, album cover, etc remain blank as before.

Here is a snippet of my code -

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagOptionSingleton;
import org.jaudiotagger.tag.id3.ID3v23Tag;
import org.jaudiotagger.tag.images.Artwork;
import org.jaudiotagger.tag.images.ArtworkFactory;

try{
    TagOptionSingleton.getInstance().setAndroid(true);
    AudioFile f =AudioFileIO.read(file_path_to_mp3);
    f.setTag(new ID3v23Tag());
    Tag tag=f.getTag();
    tag.setField(FieldKey.ARTIST,artist);
    tag.setField(FieldKey.ALBUM,album);
    Artwork cover=ArtworkFactory.createArtworkFromFile(cover_file);
    tag.setField(cover);
    f.commit();
}catch (Exception e){
    e.printStackTrace();
}

I can run the program without getting any error and exception thrown. Just that the matadata tag like title, artist, album etc wont set though the artwork can be set successfully. Thanks for any help!

1条回答
做个烂人
2楼-- · 2020-06-28 03:29

I write some code to help you change the data of the mp3 file

    TagOptionSingleton.getInstance().setAndroid(true);

    File mp3File = new File(mp3Song);

    AudioFile audioFile = AudioFileIO.read(mp3File);

    audioFile.setTag(new ID3v23Tag());


    Tag newTag=    audioFile.getTag();

    if (album != null) {
        newTag.setField(ALBUM, album);
    }
    if (artist != null) {
        newTag.setField(ARTIST, artist);
    }

    if (trackName != null) {
        Log.d("ALBUM TITLE",trackName);

        newTag.setField(TITLE, trackName);
    }


    if (imageAlbum != null) {
        Log.d("ALBUM COVER",imageAlbum);

        File fileCover = new File(imageAlbum);
        if (!fileCover.exists()){

            Log.d("ALBUM ","DOESNT EXIST");

        }


        Artwork artwork = Artwork.createArtworkFromFile(fileCover);
        newTag.addField(artwork);
        newTag.setField(artwork);

    }
    audioFile.commit();de here
查看更多
登录 后发表回答