Search in sources :

Example 36 with CannotReadException

use of org.jaudiotagger.audio.exceptions.CannotReadException in project JamsMusicPlayer by psaravan.

the class AsyncDeleteAlbumArtTask method doInBackground.

@Override
protected Void doInBackground(String... params) {
    if (params.length == 2) {
        artist = params[0];
        album = params[1];
    }
    //Remove the + and replace them back with spaces. Also replace any rogue apostrophes.
    try {
        if (album.contains("+")) {
            album = album.replace("+", " ");
        }
        if (album.contains("'")) {
            album = album.replace("'", "''");
        }
        if (artist.contains("+")) {
            artist = artist.replace("+", " ");
        }
        if (artist.contains("'")) {
            artist = artist.replace("'", "''");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    String selection = DBAccessHelper.SONG_ALBUM + "=" + "'" + album + "'" + " AND " + DBAccessHelper.SONG_ARTIST + "=" + "'" + artist + "'";
    String[] projection = { DBAccessHelper._ID, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_ALBUM_ART_PATH };
    Cursor cursor = mApp.getDBAccessHelper().getWritableDatabase().query(DBAccessHelper.MUSIC_LIBRARY_TABLE, projection, selection, null, null, null, null);
    cursor.moveToFirst();
    if (cursor.getCount() != 0) {
        dataURIsList.add(cursor.getString(1));
        albumArtPathsList.add(cursor.getString(2));
    }
    while (cursor.moveToNext()) {
        dataURIsList.add(cursor.getString(1));
        albumArtPathsList.add(cursor.getString(2));
    }
    for (int i = 0; i < dataURIsList.size(); i++) {
        File audioFile = new File(dataURIsList.get(i));
        AudioFile f = null;
        try {
            f = AudioFileIO.read(audioFile);
        } 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 tag = null;
        if (f != null) {
            tag = f.getTag();
        } else {
            continue;
        }
        try {
            tag.deleteArtworkField();
        } catch (KeyNotFoundException e) {
            Toast.makeText(mContext, R.string.album_doesnt_have_artwork, Toast.LENGTH_LONG).show();
        }
        try {
            f.commit();
        } catch (CannotWriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Check if the current song's album art is a JPEG file.
        if (albumArtPathsList.get(i).startsWith("/")) {
            File file = new File(albumArtPathsList.get(i));
            if (file != null) {
                if (file.exists()) {
                    file.delete();
                }
            }
        }
        //Remove the album art from the album art database.
        String filePath = dataURIsList.get(i);
        filePath = filePath.replace("'", "''");
        String where = DBAccessHelper.SONG_FILE_PATH + "=" + "'" + filePath + "'";
        ContentValues values = new ContentValues();
        values.put(DBAccessHelper.SONG_ALBUM_ART_PATH, "");
        mApp.getDBAccessHelper().getWritableDatabase().update(DBAccessHelper.MUSIC_LIBRARY_TABLE, values, where, null);
    }
    //Refresh the memory/disk cache.
    mApp.getImageLoader().clearDiscCache();
    mApp.getImageLoader().clearMemoryCache();
    cursor.close();
    cursor = null;
    return null;
}
Also used : CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) ContentValues(android.content.ContentValues) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Cursor(android.database.Cursor) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) KeyNotFoundException(org.jaudiotagger.tag.KeyNotFoundException) IOException(java.io.IOException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) File(java.io.File) AudioFile(org.jaudiotagger.audio.AudioFile) KeyNotFoundException(org.jaudiotagger.tag.KeyNotFoundException)

Example 37 with CannotReadException

use of org.jaudiotagger.audio.exceptions.CannotReadException in project Shuttle by timusus.

the class TaggerDialog method populateViews.

void populateViews() {
    if (originalSongPaths == null || originalSongPaths.isEmpty()) {
        return;
    }
    try {
        AudioFile mAudioFile = AudioFileIO.read(new File(originalSongPaths.get(0)));
        Tag tag = mAudioFile.getTag();
        if (tag == null) {
            return;
        }
        title = tag.getFirst(FieldKey.TITLE);
        albumName = tag.getFirst(FieldKey.ALBUM);
        artistName = tag.getFirst(FieldKey.ARTIST);
        try {
            albumArtistName = tag.getFirst(FieldKey.ALBUM_ARTIST);
        } catch (UnsupportedOperationException ignored) {
        }
        genre = tag.getFirst(FieldKey.GENRE);
        year = tag.getFirst(FieldKey.YEAR);
        track = tag.getFirst(FieldKey.TRACK);
        try {
            trackTotal = tag.getFirst(FieldKey.TRACK_TOTAL);
        } catch (UnsupportedOperationException ignored) {
        }
        try {
            disc = tag.getFirst(FieldKey.DISC_NO);
        } catch (UnsupportedOperationException ignored) {
        }
        try {
            discTotal = tag.getFirst(FieldKey.DISC_TOTAL);
        } catch (UnsupportedOperationException ignored) {
        }
        try {
            lyrics = tag.getFirst(FieldKey.LYRICS);
        } catch (UnsupportedOperationException ignored) {
        }
        try {
            comment = tag.getFirst(FieldKey.COMMENT);
        } catch (UnsupportedOperationException ignored) {
        }
    } catch (IOException | InvalidAudioFrameException | TagException | ReadOnlyFileException | CannotReadException e) {
        Log.e(TAG, "Failed to read tags. " + e.toString());
    }
    titleEditText.setText(title);
    titleEditText.setSelection(titleEditText.getText().length());
    titleEditText.addTextChangedListener(new CustomTextWatcher(titleEditText, textChangeListener));
    albumEditText.setText(albumName);
    albumEditText.setSelection(albumEditText.getText().length());
    albumEditText.addTextChangedListener(new CustomTextWatcher(titleEditText, textChangeListener));
    artistEditText.setText(artistName);
    artistEditText.setSelection(artistEditText.getText().length());
    artistEditText.addTextChangedListener(new CustomTextWatcher(artistEditText, textChangeListener));
    albumArtistEditText.setText(albumArtistName);
    albumArtistEditText.setSelection(albumArtistEditText.getText().length());
    albumArtistEditText.addTextChangedListener(new CustomTextWatcher(albumArtistEditText, textChangeListener));
    genreEditText.setText(genre);
    genreEditText.setSelection(genreEditText.getText().length());
    genreEditText.addTextChangedListener(new CustomTextWatcher(genreEditText, textChangeListener));
    yearEditText.setText(String.valueOf(year));
    yearEditText.setSelection(yearEditText.getText().length());
    yearEditText.addTextChangedListener(new CustomTextWatcher(yearEditText, textChangeListener));
    trackEditText.setText(String.valueOf(track));
    trackEditText.setSelection(trackEditText.getText().length());
    trackEditText.addTextChangedListener(new CustomTextWatcher(trackEditText, textChangeListener));
    trackTotalEditText.setText(String.valueOf(trackTotal));
    trackTotalEditText.setSelection(trackTotalEditText.getText().length());
    trackTotalEditText.addTextChangedListener(new CustomTextWatcher(trackTotalEditText, textChangeListener));
    discEditText.setText(String.valueOf(disc));
    discEditText.setSelection(discEditText.getText().length());
    discEditText.addTextChangedListener(new CustomTextWatcher(discEditText, textChangeListener));
    discTotalEditText.setText(String.valueOf(discTotal));
    discTotalEditText.setSelection(discTotalEditText.getText().length());
    discTotalEditText.addTextChangedListener(new CustomTextWatcher(discTotalEditText, textChangeListener));
    lyricsEditText.setText(lyrics);
    lyricsEditText.setSelection(lyricsEditText.getText().length());
    lyricsEditText.addTextChangedListener(new CustomTextWatcher(lyricsEditText, textChangeListener));
    commentEditText.setText(comment);
    commentEditText.setSelection(commentEditText.getText().length());
    commentEditText.addTextChangedListener(new CustomTextWatcher(commentEditText, textChangeListener));
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) 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) DocumentFile(android.support.v4.provider.DocumentFile)

Example 38 with CannotReadException

use of org.jaudiotagger.audio.exceptions.CannotReadException in project Shuttle by timusus.

the class TaggerTask method doInBackground.

@Override
protected Boolean doInBackground(Object... params) {
    boolean success = false;
    boolean requiresPermission = TaggerUtils.requiresPermission(paths);
    for (int i = 0; i < paths.size(); i++) {
        final String path = paths.get(i);
        try {
            File orig = new File(path);
            AudioFile audioFile = AudioFileIO.read(orig);
            Tag tag = audioFile.getTag();
            if (tag == null) {
                break;
            }
            TagUpdate tagUpdate = new TagUpdate(tag);
            tagUpdate.softSetArtist(artistText);
            tagUpdate.softSetAlbumArtist(albumArtistText);
            tagUpdate.softSetGenre(genreText);
            tagUpdate.softSetYear(yearText);
            if (showAlbum) {
                tagUpdate.softSetAlbum(albumText);
                tagUpdate.softSetDiscTotal(discTotalText);
            }
            if (showTrack) {
                tagUpdate.softSetTitle(titleText);
                tagUpdate.softSetTrack(trackText);
                tagUpdate.softSetTrackTotal(trackTotalText);
                tagUpdate.softSetDisc(discText);
                tagUpdate.softSetLyrics(lyricsText);
                tagUpdate.softSetComment(commentText);
            }
            File temp = null;
            if (tagUpdate.hasChanged()) {
                if (TaggerUtils.requiresPermission(paths)) {
                    temp = new File(ShuttleApplication.getInstance().getFilesDir(), orig.getName());
                    tempFiles.add(temp);
                    TaggerUtils.copyFile(orig, temp);
                    audioFile = AudioFileIO.read(temp);
                    tag = audioFile.getTag();
                    if (tag == null) {
                        break;
                    }
                }
                tagUpdate.updateTag(tag);
                AudioFileIO.write(audioFile);
                if (requiresPermission && temp != null) {
                    DocumentFile documentFile = documentFiles.get(i);
                    if (documentFile != null) {
                        ParcelFileDescriptor pfd = ShuttleApplication.getInstance().getContentResolver().openFileDescriptor(documentFile.getUri(), "w");
                        if (pfd != null) {
                            FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                            TaggerUtils.copyFile(temp, fileOutputStream);
                            pfd.close();
                        }
                        if (temp.delete()) {
                            if (tempFiles.contains(temp)) {
                                tempFiles.remove(temp);
                            }
                        }
                    }
                }
            }
            publishProgress(i);
            success = true;
        } catch (CannotWriteException | IOException | CannotReadException | InvalidAudioFrameException | TagException | ReadOnlyFileException e) {
            e.printStackTrace();
        } finally {
            // Try to clean up our temp files
            if (tempFiles != null && tempFiles.size() != 0) {
                for (int j = tempFiles.size() - 1; j >= 0; j--) {
                    File file = tempFiles.get(j);
                    file.delete();
                    tempFiles.remove(j);
                }
            }
        }
    }
    return success;
}
Also used : CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) DocumentFile(android.support.v4.provider.DocumentFile) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) TagUpdate(com.simplecity.amp_library.model.TagUpdate) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) File(java.io.File) AudioFile(org.jaudiotagger.audio.AudioFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 39 with CannotReadException

use of org.jaudiotagger.audio.exceptions.CannotReadException in project Shuttle by timusus.

the class LyricsPresenter method updateLyrics.

private void updateLyrics() {
    addDisposable(Observable.fromCallable(() -> {
        String lyrics = "";
        String path = MusicUtils.getFilePath();
        if (TextUtils.isEmpty(path)) {
            return lyrics;
        }
        if (path.startsWith("content://")) {
            Query query = new Query.Builder().uri(Uri.parse(path)).projection(new String[] { MediaStore.Audio.Media.DATA }).build();
            Cursor cursor = SqlUtils.createQuery(ShuttleApplication.getInstance(), query);
            if (cursor != null) {
                try {
                    int colIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                    if (cursor.moveToFirst()) {
                        path = cursor.getString(colIndex);
                    }
                } finally {
                    cursor.close();
                }
            }
        }
        File file = new File(path);
        if (file.exists()) {
            try {
                AudioFile audioFile = AudioFileIO.read(file);
                if (audioFile != null) {
                    Tag tag = audioFile.getTag();
                    if (tag != null) {
                        String tagLyrics = tag.getFirst(FieldKey.LYRICS);
                        if (tagLyrics != null && tagLyrics.length() != 0) {
                            lyrics = tagLyrics.replace("\r", "\n");
                        }
                    }
                }
            } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | UnsupportedOperationException ignored) {
            }
        }
        return lyrics;
    }).subscribe(lyrics -> {
        LyricsView lyricsView = getView();
        if (lyricsView != null) {
            lyricsView.updateLyrics(lyrics);
            lyricsView.showNoLyricsView(TextUtils.isEmpty(lyrics));
            lyricsView.showQuickLyricInfoButton(!QuickLyricUtils.isQLInstalled());
        }
    }, error -> LogUtils.logException(TAG, "Error getting lyrics", error)));
}
Also used : ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull) Song(com.simplecity.amp_library.model.Song) AudioFile(org.jaudiotagger.audio.AudioFile) MediaStore(android.provider.MediaStore) Observable(io.reactivex.Observable) Cursor(android.database.Cursor) FieldKey(org.jaudiotagger.tag.FieldKey) LogUtils(com.simplecity.amp_library.utils.LogUtils) BackpressureStrategy(io.reactivex.BackpressureStrategy) IntentFilter(android.content.IntentFilter) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Tag(org.jaudiotagger.tag.Tag) RxBroadcast(com.cantrowitz.rxbroadcast.RxBroadcast) File(java.io.File) Query(com.simplecity.amp_library.model.Query) Presenter(com.simplecity.amp_library.ui.presenters.Presenter) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) MusicService(com.simplecity.amp_library.playback.MusicService) TagException(org.jaudiotagger.tag.TagException) AudioFileIO(org.jaudiotagger.audio.AudioFileIO) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) SqlUtils(com.simplecity.amp_library.sql.SqlUtils) Query(com.simplecity.amp_library.model.Query) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Cursor(android.database.Cursor) 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)

Aggregations

CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)39 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)23 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)23 TagException (org.jaudiotagger.tag.TagException)22 IOException (java.io.IOException)21 AudioFile (org.jaudiotagger.audio.AudioFile)18 File (java.io.File)15 Tag (org.jaudiotagger.tag.Tag)14 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)12 Cursor (android.database.Cursor)6 Paint (android.graphics.Paint)6 ByteBuffer (java.nio.ByteBuffer)5 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)5 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)5 ContentValues (android.content.ContentValues)4 GenericAudioHeader (org.jaudiotagger.audio.generic.GenericAudioHeader)4 OggPageHeader (org.jaudiotagger.audio.ogg.util.OggPageHeader)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 OnClickListener (android.content.DialogInterface.OnClickListener)3