Search in sources :

Example 11 with TagException

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());
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) Genre(com.simplecity.amp_library.model.Genre) PackageManager(android.content.pm.PackageManager) CrashlyticsCore(com.crashlytics.android.core.CrashlyticsCore) Completable(io.reactivex.Completable) PlayCountTable(com.simplecity.amp_library.sql.providers.PlayCountTable) CastConfiguration(com.google.android.libraries.cast.companionlibrary.cast.CastConfiguration) AnalyticsManager(com.simplecity.amp_library.utils.AnalyticsManager) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) Manifest(android.Manifest) DaggerAppComponent(com.simplecity.amp_library.dagger.component.DaggerAppComponent) AudioFile(org.jaudiotagger.audio.AudioFile) MediaStore(android.provider.MediaStore) Schedulers(io.reactivex.schedulers.Schedulers) Log(android.util.Log) FirebaseAnalytics(com.google.firebase.analytics.FirebaseAnalytics) FieldKey(org.jaudiotagger.tag.FieldKey) LogUtils(com.simplecity.amp_library.utils.LogUtils) ContextCompat(android.support.v4.content.ContextCompat) VideoCastManager(com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager) Logger(java.util.logging.Logger) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) PreferenceManager(android.support.v7.preference.PreferenceManager) Query(com.simplecity.amp_library.model.Query) List(java.util.List) Config(com.simplecity.amp_library.constants.Config) Application(android.app.Application) TagException(org.jaudiotagger.tag.TagException) AudioFileIO(org.jaudiotagger.audio.AudioFileIO) Fabric(io.fabric.sdk.android.Fabric) CompletableTransformer(io.reactivex.CompletableTransformer) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) ContentValues(android.content.ContentValues) Answers(com.crashlytics.android.answers.Answers) SqlUtils(com.simplecity.amp_library.sql.SqlUtils) AppModule(com.simplecity.amp_library.dagger.module.AppModule) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagOptionSingleton(org.jaudiotagger.tag.TagOptionSingleton) Stream(com.annimon.stream.Stream) Environment(android.os.Environment) RefWatcher(com.squareup.leakcanary.RefWatcher) HashMap(java.util.HashMap) NonNull(android.support.annotation.NonNull) StringUtils(com.simplecity.amp_library.utils.StringUtils) InputMethodManagerLeaks(com.simplecity.amp_library.utils.InputMethodManagerLeaks) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Observable(io.reactivex.Observable) AppComponent(com.simplecity.amp_library.dagger.component.AppComponent) SqlBriteUtils(com.simplecity.amp_library.sql.sqlbrite.SqlBriteUtils) Collectors(com.annimon.stream.Collectors) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Tag(org.jaudiotagger.tag.Tag) CustomArtworkTable(com.simplecity.amp_library.sql.databases.CustomArtworkTable) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Glide(com.bumptech.glide.Glide) SharedPreferences(android.content.SharedPreferences) StrictMode(android.os.StrictMode) Crashlytics(com.crashlytics.android.Crashlytics) DataManager(com.simplecity.amp_library.utils.DataManager) LeakCanary(com.squareup.leakcanary.LeakCanary) LegacyUtils(com.simplecity.amp_library.utils.LegacyUtils) Collections(java.util.Collections) ContentUris(android.content.ContentUris) FirebaseApp(com.google.firebase.FirebaseApp) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) NonNull(android.support.annotation.NonNull)

Example 12 with TagException

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()));
        }
    }
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) RandomAccessFile(java.io.RandomAccessFile) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) Tag(org.jaudiotagger.tag.Tag) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException)

Example 13 with TagException

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;
}
Also used : TagException(org.jaudiotagger.tag.TagException) AbstractID3v2Tag(org.jaudiotagger.tag.id3.AbstractID3v2Tag) ID3v22Tag(org.jaudiotagger.tag.id3.ID3v22Tag) ID3v24Tag(org.jaudiotagger.tag.id3.ID3v24Tag) ID3v23Tag(org.jaudiotagger.tag.id3.ID3v23Tag) ByteBuffer(java.nio.ByteBuffer)

Example 14 with TagException

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;
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) IOException(java.io.IOException) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File)

Example 15 with TagException

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;
    }
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) Cursor(android.database.Cursor) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) Paint(android.graphics.Paint) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) FieldDataInvalidException(org.jaudiotagger.tag.FieldDataInvalidException) NoSuchElementException(java.util.NoSuchElementException) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) KeyNotFoundException(org.jaudiotagger.tag.KeyNotFoundException) IOException(java.io.IOException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)

Aggregations

TagException (org.jaudiotagger.tag.TagException)21 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)20 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)20 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)20 IOException (java.io.IOException)19 AudioFile (org.jaudiotagger.audio.AudioFile)16 File (java.io.File)13 Tag (org.jaudiotagger.tag.Tag)13 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)8 Cursor (android.database.Cursor)6 Paint (android.graphics.Paint)5 ContentValues (android.content.ContentValues)4 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)4 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 OnClickListener (android.content.DialogInterface.OnClickListener)3 CompoundButton (android.widget.CompoundButton)3 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)3 Query (com.simplecity.amp_library.model.Query)3