Search in sources :

Example 16 with Tag

use of org.jaudiotagger.tag.Tag 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 17 with Tag

use of org.jaudiotagger.tag.Tag in project JamsMusicPlayer by psaravan.

the class AsyncBuildFoldersCursorTask method doInBackground.

@Override
protected Boolean doInBackground(String... params) {
    //We'll create a matrix cursor that includes all the audio files within the specified folder.
    String[] foldersCursorColumns = { DBAccessHelper.SONG_ARTIST, DBAccessHelper.SONG_ALBUM, DBAccessHelper.SONG_TITLE, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_DURATION, DBAccessHelper.SONG_GENRE, DBAccessHelper.SONG_SOURCE, DBAccessHelper.SONG_ALBUM_ART_PATH, DBAccessHelper.SONG_ID, DBAccessHelper.LOCAL_COPY_PATH };
    MatrixCursor foldersCursor = new MatrixCursor(foldersCursorColumns);
    String artist = "";
    String album = "";
    String title = "";
    String filePath = "";
    String duration = "";
    String genre = "";
    String songSource = "LOCAL_FILE";
    String songAlbumArtPath = "";
    String songId = "";
    for (int i = 0; i < mSongFilePathsList.size(); i++) {
        if (mSongFilePathsList.size() <= 5 && i == 5) {
            mApp.getService().setCursor((Cursor) foldersCursor);
        }
        try {
            File file = new File(mSongFilePathsList.get(i));
            AudioFile audioFile = AudioFileIO.read(file);
            Tag tag = audioFile.getTag();
            filePath = mSongFilePathsList.get(i);
            artist = tag.getFirst(FieldKey.ARTIST);
            if (artist == null || artist.equals(" ") || artist.isEmpty()) {
                artist = "Unknown Artist";
            }
            album = tag.getFirst(FieldKey.ALBUM);
            if (album == null || album.equals(" ") || album.isEmpty()) {
                album = "Unknown Album";
            }
            title = tag.getFirst(FieldKey.ARTIST);
            if (title == null || title.equals(" ") || title.isEmpty()) {
                title = filePath;
            }
            duration = "" + audioFile.getAudioHeader().getTrackLength();
            if (duration == null || duration.equals(" ") || duration.isEmpty()) {
                duration = "0";
            }
            genre = tag.getFirst(FieldKey.GENRE);
            if (genre == null || genre.equals(" ") || genre.isEmpty()) {
                genre = "Unknown Genre";
            }
            foldersCursor.addRow(new Object[] { artist, album, title, filePath, duration, genre, songSource, songAlbumArtPath, songId, "" });
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
    }
    //cursor = (Cursor) foldersCursor;
    mApp.getService().setCursor((Cursor) foldersCursor);
    return null;
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) MatrixCursor(android.database.MatrixCursor)

Example 18 with Tag

use of org.jaudiotagger.tag.Tag in project saga-android by AnandChowdhary.

the class DownloadReceiver method onReceive.

@Override
public void onReceive(final Context context, final Intent intent) {
    DownloadManager dMgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Long downloadId = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
    Cursor c = dMgr.query(new DownloadManager.Query().setFilterById(downloadId));
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            final String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
            Log.d("Receiver", "Title:" + title);
            if (title.equalsIgnoreCase(context.getString(R.string.app_name) + " " + context.getString(R.string.update))) {
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setDataAndType(Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))), "application/vnd.android.package-archive");
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(install);
            } else {
                try {
                    TagOptionSingleton.getInstance().setAndroid(true);
                    final File file = new File(Utils.getStoragePath(context) + "/" + title);
                    final AudioFile f = AudioFileIO.read(file);
                    final Tag tag = f.getTag();
                    String json = readFromFile(context, title);
                    String url = null;
                    if (json != null) {
                        JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.getString("track") != null) {
                            if (jsonObject.getString("artist") != null) {
                                url = Utils.getAlbumArt(jsonObject.getString("track"), jsonObject.getString("artist"));
                            } else {
                                url = Utils.getAlbumArt(jsonObject.getString("track"), null);
                            }
                        }
                        if (jsonObject.getString("artist") != null)
                            tag.setField(FieldKey.ARTIST, jsonObject.getString("artist"));
                        if (jsonObject.getString("artist") != null)
                            tag.setField(FieldKey.ALBUM_ARTIST, jsonObject.getString("artist"));
                        if (jsonObject.getString("release") != null)
                            tag.setField(FieldKey.YEAR, jsonObject.getString("release"));
                        if (jsonObject.getString("trackno") != null)
                            tag.setField(FieldKey.TRACK, jsonObject.getString("trackno"));
                        if (jsonObject.getString("album") != null)
                            tag.setField(FieldKey.ALBUM, jsonObject.getString("album"));
                        if (jsonObject.getString("genre") != null)
                            tag.setField(FieldKey.GENRE, jsonObject.getString("genre"));
                        tag.setField(FieldKey.COMMENT, "Downloaded from Saga");
                    } else {
                        url = Utils.getAlbumArt(title.substring(0, title.length() - 4), null);
                    }
                    if (url != null) {
                        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {

                            @Override
                            public void onResponse(Bitmap bitmap) {
                                FileOutputStream out = null;
                                try {
                                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                                    String imageFileName = "JPEG_" + timeStamp + "_";
                                    File storageDir = context.getCacheDir();
                                    File cover = File.createTempFile(imageFileName, /* prefix */
                                    ".jpg", /* suffix */
                                    storageDir);
                                    out = new FileOutputStream(cover);
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                                    AndroidArtwork artwork = AndroidArtwork.createArtworkFromFile(cover);
                                    tag.setField(artwork);
                                    Log.d(TAG, "AlbumArt deleted " + cover.delete());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                } finally {
                                    commitAudio(context, f, file);
                                    try {
                                        if (out != null) {
                                            out.close();
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }, 0, 0, null, new Response.ErrorListener() {

                            public void onErrorResponse(VolleyError error) {
                                error.printStackTrace();
                                commitAudio(context, f, file);
                            }
                        });
                        request.setShouldCache(false);
                        VolleySingleton.getInstance(context).addToRequestQueue(request);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : VolleyError(com.android.volley.VolleyError) Intent(android.content.Intent) IOException(java.io.IOException) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) Date(java.util.Date) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Response(com.android.volley.Response) AudioFile(org.jaudiotagger.audio.AudioFile) Bitmap(android.graphics.Bitmap) JSONObject(org.json.JSONObject) AndroidArtwork(org.jaudiotagger.tag.images.AndroidArtwork) ImageRequest(com.android.volley.toolbox.ImageRequest) FileOutputStream(java.io.FileOutputStream) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 19 with Tag

use of org.jaudiotagger.tag.Tag in project libresonic by Libresonic.

the class JaudiotaggerParser method setMetaData.

/**
     * Updates the given file with the given meta data.
     *
     * @param file     The music file to update.
     * @param metaData The new meta data.
     */
@Override
public void setMetaData(MediaFile file, MetaData metaData) {
    try {
        AudioFile audioFile = AudioFileIO.read(file.getFile());
        Tag tag = audioFile.getTagOrCreateAndSetDefault();
        tag.setField(FieldKey.ARTIST, StringUtils.trimToEmpty(metaData.getArtist()));
        tag.setField(FieldKey.ALBUM, StringUtils.trimToEmpty(metaData.getAlbumName()));
        tag.setField(FieldKey.TITLE, StringUtils.trimToEmpty(metaData.getTitle()));
        tag.setField(FieldKey.GENRE, StringUtils.trimToEmpty(metaData.getGenre()));
        try {
            tag.setField(FieldKey.ALBUM_ARTIST, StringUtils.trimToEmpty(metaData.getAlbumArtist()));
        } catch (Exception x) {
        // Silently ignored. ID3v1 doesn't support album artist.
        }
        Integer track = metaData.getTrackNumber();
        if (track == null) {
            tag.deleteField(FieldKey.TRACK);
        } else {
            tag.setField(FieldKey.TRACK, String.valueOf(track));
        }
        Integer year = metaData.getYear();
        if (year == null) {
            tag.deleteField(FieldKey.YEAR);
        } else {
            tag.setField(FieldKey.YEAR, String.valueOf(year));
        }
        audioFile.commit();
    } catch (Throwable x) {
        LOG.warn("Failed to update tags for file " + file, x);
        throw new RuntimeException("Failed to update tags for file " + file + ". " + x.getMessage(), x);
    }
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) Tag(org.jaudiotagger.tag.Tag)

Example 20 with Tag

use of org.jaudiotagger.tag.Tag 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)

Aggregations

Tag (org.jaudiotagger.tag.Tag)23 AudioFile (org.jaudiotagger.audio.AudioFile)21 IOException (java.io.IOException)17 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)16 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)16 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)16 File (java.io.File)15 TagException (org.jaudiotagger.tag.TagException)15 Cursor (android.database.Cursor)7 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)7 ContentValues (android.content.ContentValues)5 MediaStore (android.provider.MediaStore)4 NonNull (android.support.annotation.NonNull)4 Query (com.simplecity.amp_library.model.Query)4 AudioFileIO (org.jaudiotagger.audio.AudioFileIO)4 ContentUris (android.content.ContentUris)3 Log (android.util.Log)3 FileOutputStream (java.io.FileOutputStream)3 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)3 Manifest (android.Manifest)2