How to use JaudioTagger to write data?

1.2k Views Asked by At

I am trying to use JaudioTagger 2.2.3 to edit tags of audio file but till now i have not achieved any success. this is a test code to change the Artist tag of a mp3 file in Internal Storage

String path;
try
{
  TagOptionSingleton.getInstance().setAndroid(true);
  AudioFile f=AudioFileIO.read(new File(path));
  Tag t=f.getTag();
  t.setField(FieldKey.ARTIST,Str);
  AudioFileIO.write(f);
}catch(CannotReadException e){}
 catch(TagException e){}
 catch(ReadOnlyException e){}
 catch(InvalidAudioFrameException e){}
 catch(CannotWriteException e){}

My Application is well elevated with android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE I am testing in Android version Android 6.0 this code throws InvalidAudioFrameException with a message

No audio header found within example.mp3

Its not that the audio is corrupt as it shows similar error with other mp3s also Also if any one has any other wat for audio tagging please do tell me and i have also used mp3agic which repeadedly shows:example.mp3:open failed :EROFS(Read only file system) on the line

Mp3File mp3file = new Mp3File(filePathStr+"example.mp3");
ID3v1 id3v1Tag;
if (mp3file.hasId3v1Tag()) {
  id3v1Tag =  mp3file.getId3v1Tag();
} else {
  // mp3 does not have an ID3v1 tag, let's create one..
  id3v1Tag = new ID3v1Tag();
  mp3file.setId3v1Tag(id3v1Tag);
    }
  id3v1Tag.setTrack("5");
  id3v1Tag.setArtist("An Artist");
  id3v1Tag.setTitle("The Title");
  id3v1Tag.setAlbum("The Album");
  id3v1Tag.setYear("2001");
  id3v1Tag.setGenre(12);
  id3v1Tag.setComment("Some comment");
  mp3file.save("example.mp3")//error showing in this line

My question

  • How to rectify Jaudiotagger?
  • Is there any way to use Mp3agic as an alternative to Jaudiotagger?
  • Or there is any other efficient way leaving these two?

I have also used JaudioTagger-android but same problem persists. thanks in advance!

1

There are 1 best solutions below

0
On

For the error when doing

mp3file.save("example.mp3")

i would recommend to not overwrite directly the old file. Try to create a new file, and then delete and rename to restore the original file setup.

song.save(location + "intermed");
File from = new File(location + "intermed");
File file = new File(location);
file.delete();
File to = new File(location);
from.renameTo(to);

Of course you need to add exception handling and what not, and also this will fail if you have no write permissions. This may be the case if you use a more recent Android version, i think above 4.4, and you try to access the SD card. Then, the write permissions need to be granted at runtime for specific folders, the manifest write permission is not sufficient. See Android's Storage Access Framework.