use of org.jaudiotagger.tag.TagException in project Shuttle by timusus.
the class ShuttleApplication method repairMediaStoreYearFromTags.
@NonNull
private Completable repairMediaStoreYearFromTags() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return Completable.complete();
}
return DataManager.getInstance().getSongsObservable(value -> value.year < 1).first(Collections.emptyList()).map(songs -> Stream.of(songs).flatMap(song -> {
if (!TextUtils.isEmpty(song.path)) {
File file = new File(song.path);
if (file.exists()) {
try {
AudioFile audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag();
if (tag != null) {
String year = tag.getFirst(FieldKey.YEAR);
int yearInt = StringUtils.parseInt(year);
if (yearInt > 0) {
song.year = yearInt;
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Audio.Media.YEAR, yearInt);
return Stream.of(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, song.id)).withValues(contentValues).build());
}
}
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
}
}
}
return Stream.empty();
}).collect(Collectors.toCollection(ArrayList::new))).doOnSuccess(contentProviderOperations -> getContentResolver().applyBatch(MediaStore.AUTHORITY, contentProviderOperations)).flatMapCompletable(songs -> Completable.complete());
}
use of org.jaudiotagger.tag.TagException in project MusicDNA by harjot-oberai.
the class AudioFileReader method read.
/*
* Reads the given file, and return an AudioFile object containing the Tag
* and the encoding infos present in the file. If the file has no tag, an
* empty one is returned. If the encodinginfo is not valid , an exception is thrown.
*
* @param f The file to read
* @exception CannotReadException If anything went bad during the read of this file
*/
public AudioFile read(File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
if (logger.isLoggable(Level.CONFIG)) {
logger.config(ErrorMessage.GENERAL_READ.getMsg(f.getAbsolutePath()));
}
if (!f.canRead()) {
throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
}
if (f.length() <= MINIMUM_SIZE_FOR_VALID_AUDIO_FILE) {
throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
}
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(f, "r");
raf.seek(0);
GenericAudioHeader info = getEncodingInfo(raf);
raf.seek(0);
Tag tag = getTag(raf);
return new AudioFile(f, info, tag);
} catch (CannotReadException cre) {
throw cre;
} catch (Exception e) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_READ.getMsg(f.getAbsolutePath()), e);
throw new CannotReadException(f.getAbsolutePath() + ":" + e.getMessage(), e);
} finally {
try {
if (raf != null) {
raf.close();
}
} catch (Exception ex) {
logger.log(Level.WARNING, ErrorMessage.GENERAL_READ_FAILED_UNABLE_TO_CLOSE_RANDOM_ACCESS_FILE.getMsg(f.getAbsolutePath()));
}
}
}
use of org.jaudiotagger.tag.TagException in project MusicDNA by harjot-oberai.
the class ID3Chunk method readChunk.
@Override
public boolean readChunk() throws IOException {
// TODO Auto-generated method stub
if (!isId3v2Tag()) {
// Bad ID3V2 tag
return false;
}
int version = raf.read();
AbstractID3v2Tag id3Tag;
switch(version) {
case 2:
id3Tag = new ID3v22Tag();
AudioFile.logger.finest("Reading ID3V2.2 tag");
break;
case 3:
id3Tag = new ID3v23Tag();
AudioFile.logger.finest("Reading ID3V2.3 tag");
break;
case 4:
id3Tag = new ID3v24Tag();
AudioFile.logger.finest("Reading ID3V2.4 tag");
break;
default:
// bad or unknown version
return false;
}
aiffTag.setID3Tag(id3Tag);
// back up to start of tag
raf.seek(raf.getFilePointer() - 4);
byte[] buf = new byte[(int) bytesLeft];
raf.read(buf);
ByteBuffer bb = ByteBuffer.allocate((int) bytesLeft);
bb.put(buf);
try {
id3Tag.read(bb);
} catch (TagException e) {
AudioFile.logger.info("Exception reading ID3 tag: " + e.getClass().getName() + ": " + e.getMessage());
return false;
}
return true;
}
use of org.jaudiotagger.tag.TagException in project JamsMusicPlayer by psaravan.
the class WriteToM3UPlaylist method getSongTitle.
public String getSongTitle(String songFilePath) {
String songTitle = "Title";
//Try to get the song title from the ID3 tag.
File songFile = new File(songFilePath);
AudioFile audioFile = null;
Tag tag = null;
try {
audioFile = AudioFileIO.read(songFile);
} catch (CannotReadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TagException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReadOnlyFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tag = audioFile.getTag();
songTitle = tag.getFirst(FieldKey.TITLE);
//If the ID3 tag doesn't give us the info we need, just use the file name.
if (songTitle.equals("Title") || songTitle.isEmpty()) {
int indexOfLastSlash = songTitle.lastIndexOf("/") + 1;
int indexOfLastDot = songTitle.lastIndexOf(".");
songTitle = songTitle.substring(indexOfLastSlash, indexOfLastDot);
}
return songTitle;
}
use of org.jaudiotagger.tag.TagException in project JamsMusicPlayer by psaravan.
the class ID3sAlbumEditorDialog method getSongTags.
//This method loops through all the songs and saves their tags into ArrayLists.
public void getSongTags(ArrayList<String> dataURIsList) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
Cursor cursor = null;
for (int i = 0; i < dataURIsList.size(); i++) {
//Check if the song is from Google Play Music.
if (songSourcesList.get(i).equals(DBAccessHelper.GMUSIC)) {
String songId = songIdsList.get(i);
cursor = mApp.getDBAccessHelper().getSongById(songId);
cursor.moveToFirst();
titlesList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_TITLE)));
artistsList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_ARTIST)));
albumsList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_ALBUM)));
albumArtistsList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_ALBUM_ARTIST)));
genresList.add("");
producersList.add("");
yearsList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_YEAR)));
trackNumbersList.add(cursor.getString(cursor.getColumnIndex(DBAccessHelper.SONG_TRACK_NUMBER)));
totalTracksList.add("");
commentsList.add("");
} else {
File file = null;
try {
file = new File(dataURIsList.get(i));
} catch (Exception e) {
e.printStackTrace();
continue;
}
AudioFile audioFile = AudioFileIO.read(file);
titlesList.add(audioFile.getTag().getFirst(FieldKey.TITLE));
artistsList.add(audioFile.getTag().getFirst(FieldKey.ARTIST));
albumsList.add(audioFile.getTag().getFirst(FieldKey.ALBUM));
albumArtistsList.add(audioFile.getTag().getFirst(FieldKey.ALBUM_ARTIST));
genresList.add(audioFile.getTag().getFirst(FieldKey.GENRE));
producersList.add(audioFile.getTag().getFirst(FieldKey.PRODUCER));
yearsList.add(audioFile.getTag().getFirst(FieldKey.YEAR));
trackNumbersList.add(audioFile.getTag().getFirst(FieldKey.TRACK));
totalTracksList.add(audioFile.getTag().getFirst(FieldKey.TRACK_TOTAL));
commentsList.add(audioFile.getTag().getFirst(FieldKey.COMMENT));
}
}
if (cursor != null) {
cursor.close();
cursor = null;
}
}
Aggregations