Search in sources :

Example 16 with ReadOnlyFileException

use of org.jaudiotagger.audio.exceptions.ReadOnlyFileException 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)

Example 17 with ReadOnlyFileException

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

the class ID3sArtistEditorDialog method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mContext = getActivity();
    parentActivity = getActivity();
    dialogFragment = this;
    //Get the artist name.
    ARTIST = getArguments().getString("ARTIST");
    rootView = (View) parentActivity.getLayoutInflater().inflate(R.layout.fragment_edit_id3_artist_album_dialog, null);
    titleText = (TextView) rootView.findViewById(R.id.edit_title_text);
    artistText = (TextView) rootView.findViewById(R.id.edit_artist_text);
    albumText = (TextView) rootView.findViewById(R.id.edit_album_text);
    albumArtistText = (TextView) rootView.findViewById(R.id.edit_album_artist_text);
    genreText = (TextView) rootView.findViewById(R.id.edit_genre_text);
    producerText = (TextView) rootView.findViewById(R.id.edit_producer_text);
    yearText = (TextView) rootView.findViewById(R.id.edit_year_text);
    trackText = (TextView) rootView.findViewById(R.id.edit_track_text);
    ofText = (TextView) rootView.findViewById(R.id.text_of);
    commentsText = (TextView) rootView.findViewById(R.id.edit_comment_text);
    titleEditText = (EditText) rootView.findViewById(R.id.edit_title_field);
    artistEditText = (EditText) rootView.findViewById(R.id.edit_artist_field);
    albumEditText = (EditText) rootView.findViewById(R.id.edit_album_field);
    albumArtistEditText = (EditText) rootView.findViewById(R.id.edit_album_artist_field);
    genreEditText = (EditText) rootView.findViewById(R.id.edit_genre_field);
    producerEditText = (EditText) rootView.findViewById(R.id.edit_producer_field);
    yearEditText = (EditText) rootView.findViewById(R.id.edit_year_field);
    trackEditText = (EditText) rootView.findViewById(R.id.edit_track_field);
    trackTotalEditText = (EditText) rootView.findViewById(R.id.edit_track_total_field);
    commentsEditText = (EditText) rootView.findViewById(R.id.edit_comment_field);
    titleCheckbox = (CheckBox) rootView.findViewById(R.id.title_checkbox);
    artistCheckbox = (CheckBox) rootView.findViewById(R.id.artist_checkbox);
    albumCheckbox = (CheckBox) rootView.findViewById(R.id.album_checkbox);
    albumArtistCheckbox = (CheckBox) rootView.findViewById(R.id.album_artist_checkbox);
    genreCheckbox = (CheckBox) rootView.findViewById(R.id.genre_checkbox);
    producerCheckbox = (CheckBox) rootView.findViewById(R.id.producer_checkbox);
    yearCheckbox = (CheckBox) rootView.findViewById(R.id.year_checkbox);
    trackCheckbox = (CheckBox) rootView.findViewById(R.id.track_checkbox);
    commentCheckbox = (CheckBox) rootView.findViewById(R.id.comment_checkbox);
    titleText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    artistText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    albumText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    albumArtistText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    genreText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    producerText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    yearText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    trackText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    ofText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    commentsText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    titleText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    artistText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    albumText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    albumArtistText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    genreText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    producerText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    yearText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    trackText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    ofText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    commentsText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    titleEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    artistEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    albumEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    albumArtistEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    genreEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    producerEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    yearEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    trackEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    trackTotalEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    commentsEditText.setTypeface(TypefaceHelper.getTypeface(parentActivity, "RobotoCondensed-Light"));
    titleEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    artistEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    albumEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    albumArtistEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    genreEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    producerEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    yearEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    trackEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    trackTotalEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    commentsEditText.setPaintFlags(titleText.getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    //Keep all the fields locked by default.
    titleCheckbox.setChecked(false);
    artistCheckbox.setChecked(false);
    albumCheckbox.setChecked(false);
    albumArtistCheckbox.setChecked(false);
    genreCheckbox.setChecked(false);
    producerCheckbox.setChecked(false);
    yearCheckbox.setChecked(false);
    trackCheckbox.setChecked(false);
    commentCheckbox.setChecked(false);
    //Disable all EditTexts by default.
    titleEditText.setEnabled(false);
    artistEditText.setEnabled(false);
    albumEditText.setEnabled(false);
    albumArtistEditText.setEnabled(false);
    genreEditText.setEnabled(false);
    producerEditText.setEnabled(false);
    yearEditText.setEnabled(false);
    trackEditText.setEnabled(false);
    commentsEditText.setEnabled(false);
    //Register click registers on each checkbox.
    titleCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                titleEdited = true;
                titleEditText.setEnabled(true);
            } else {
                titleEdited = false;
                titleEditText.setEnabled(false);
            }
        }
    });
    artistCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                artistEdited = true;
                artistEditText.setEnabled(true);
                ;
            } else {
                artistEdited = false;
                artistEditText.setEnabled(false);
            }
        }
    });
    albumArtistCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                albumEdited = true;
                albumEditText.setEnabled(true);
                ;
            } else {
                albumEdited = false;
                albumEditText.setEnabled(false);
            }
        }
    });
    albumCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                albumArtistEdited = true;
                albumArtistEditText.setEnabled(true);
                ;
            } else {
                albumArtistEdited = false;
                albumArtistEditText.setEnabled(false);
            }
        }
    });
    genreCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                genreEdited = true;
                genreEditText.setEnabled(true);
                ;
            } else {
                genreEdited = false;
                genreEditText.setEnabled(false);
            }
        }
    });
    producerCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                producerEdited = true;
                producerEditText.setEnabled(true);
                ;
            } else {
                producerEdited = false;
                producerEditText.setEnabled(false);
            }
        }
    });
    yearCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                yearEdited = true;
                yearEditText.setEnabled(true);
                ;
            } else {
                yearEdited = false;
                yearEditText.setEnabled(false);
            }
        }
    });
    trackCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                trackEdited = true;
                trackEditText.setEnabled(true);
                ;
            } else {
                trackEdited = false;
                trackEditText.setEnabled(false);
            }
        }
    });
    commentCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean checked) {
            if (checked == true) {
                commentEdited = true;
                commentsEditText.setEnabled(true);
                ;
            } else {
                commentEdited = false;
                commentsEditText.setEnabled(false);
            }
        }
    });
    if (ARTIST != null) {
        songURIsList = getAllSongsByArtist(ARTIST);
        //Populate the ArrayLists with the song tags.
        try {
            getSongTags(songURIsList);
        } 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();
        }
        //Now check if any of the ArrayLists contain the same exact elements.
        boolean titlesSame = checkIfAllElementsEqual(titlesList);
        boolean artistsSame = checkIfAllElementsEqual(artistsList);
        boolean albumsSame = checkIfAllElementsEqual(albumsList);
        boolean albumArtistsSame = checkIfAllElementsEqual(albumArtistsList);
        boolean genresSame = checkIfAllElementsEqual(genresList);
        boolean producersSame = checkIfAllElementsEqual(producersList);
        boolean yearsSame = checkIfAllElementsEqual(yearsList);
        boolean tracksSame = checkIfAllElementsEqual(trackNumbersList);
        boolean totalTracksSame = checkIfAllElementsEqual(totalTracksList);
        boolean commentsSame = checkIfAllElementsEqual(commentsList);
        //Populate the EditTexts.
        setEditorFields(titlesSame, titlesList, titleEditText);
        setEditorFields(artistsSame, artistsList, artistEditText);
        setEditorFields(albumsSame, albumsList, albumEditText);
        setEditorFields(albumArtistsSame, albumArtistsList, albumArtistEditText);
        setEditorFields(genresSame, genresList, genreEditText);
        setEditorFields(producersSame, producersList, producerEditText);
        setEditorFields(yearsSame, yearsList, yearEditText);
        setEditorFields(tracksSame, trackNumbersList, trackEditText);
        setEditorFields(totalTracksSame, totalTracksList, trackTotalEditText);
        setEditorFields(commentsSame, commentsList, commentsEditText);
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    //Set the dialog title.
    builder.setTitle(R.string.edit_tags);
    builder.setView(rootView);
    builder.setPositiveButton(R.string.save, new OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            dialogFragment.dismiss();
            AsyncSaveArtistTagsTask asyncSaveArtistTagsTask = new AsyncSaveArtistTagsTask(getActivity(), getActivity());
            asyncSaveArtistTagsTask.execute();
        }
    });
    builder.setNegativeButton(R.string.cancel, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    return builder.create();
}
Also used : AlertDialog(android.app.AlertDialog) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) DialogInterface(android.content.DialogInterface) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Paint(android.graphics.Paint) TagException(org.jaudiotagger.tag.TagException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) OnClickListener(android.content.DialogInterface.OnClickListener) CompoundButton(android.widget.CompoundButton)

Example 18 with ReadOnlyFileException

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

the class AsyncAutoGetAlbumArtTask method doInBackground.

@Override
protected Void doInBackground(String... params) {
    //First, we'll go through all the songs in the music library DB and get their attributes.
    dbHelper = new DBAccessHelper(mContext);
    String selection = DBAccessHelper.SONG_SOURCE + "<>" + "'GOOGLE_PLAY_MUSIC'";
    String[] projection = { DBAccessHelper._ID, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_ALBUM, DBAccessHelper.SONG_ARTIST, DBAccessHelper.SONG_TITLE };
    Cursor cursor = dbHelper.getWritableDatabase().query(DBAccessHelper.MUSIC_LIBRARY_TABLE, projection, selection, null, null, null, null);
    if (cursor.getCount() != 0) {
        cursor.moveToFirst();
        dataURIsList.add(cursor.getString(1));
        albumsList.add(cursor.getString(2));
        artistsList.add(cursor.getString(3));
        while (cursor.moveToNext()) {
            dataURIsList.add(cursor.getString(1));
            albumsList.add(cursor.getString(2));
            artistsList.add(cursor.getString(3));
        }
    } else {
        //The user doesn't have any music so let's get outta here.
        return null;
    }
    pd.setMax(dataURIsList.size());
    //Now that we have the attributes of the songs, we'll go through them each and check for missing covers.
    for (int i = 0; i < dataURIsList.size(); i++) {
        try {
            file = new File(dataURIsList.get(i));
        } catch (Exception e) {
            continue;
        }
        audioFile = null;
        try {
            audioFile = AudioFileIO.read(file);
        } catch (CannotReadException e2) {
            // TODO Auto-generated catch block
            continue;
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            continue;
        } catch (TagException e2) {
            // TODO Auto-generated catch block
            continue;
        } catch (ReadOnlyFileException e2) {
            // TODO Auto-generated catch block
            continue;
        } catch (InvalidAudioFrameException e2) {
            // TODO Auto-generated catch block
            continue;
        }
        Tag tag = audioFile.getTag();
        //Set the destination directory for the xml file.
        File SDCardRoot = Environment.getExternalStorageDirectory();
        File xmlFile = new File(SDCardRoot, "albumArt.xml");
        if (tag != null) {
            String title = tag.getFirst(FieldKey.TITLE);
            String checkingMessage = mContext.getResources().getString(R.string.checking_if) + " " + title + " " + mContext.getResources().getString(R.string.has_album_art) + ".";
            currentProgress = currentProgress + 1;
            String[] checkingProgressParams = { checkingMessage, "" + currentProgress };
            publishProgress(checkingProgressParams);
            List<Artwork> artworkList = tag.getArtworkList();
            if (artworkList.size() == 0) {
                //Since the file doesn't have any album artwork, we'll have to download it.
                //Get the artist and album name of the file we're working with.
                String artist = tag.getFirst(FieldKey.ARTIST);
                String album = tag.getFirst(FieldKey.ALBUM);
                //Update the progress dialog.
                String message = mContext.getResources().getString(R.string.downloading_artwork_for) + " " + title;
                String[] progressParams = { message, "" + currentProgress };
                publishProgress(progressParams);
                //Remove any unacceptable characters.
                if (artist.contains("#")) {
                    artist = artist.replace("#", "");
                }
                if (artist.contains("$")) {
                    artist = artist.replace("$", "");
                }
                if (artist.contains("@")) {
                    artist = artist.replace("@", "");
                }
                if (album.contains("#")) {
                    album = album.replace("#", "");
                }
                if (album.contains("$")) {
                    album = album.replace("$", "");
                }
                if (album.contains("@")) {
                    album = album.replace("@", "");
                }
                //Replace any spaces in the artist and album fields with "%20".
                if (artist.contains(" ")) {
                    artist = artist.replace(" ", "%20");
                }
                if (album.contains(" ")) {
                    album = album.replace(" ", "%20");
                }
                //Construct the url for the HTTP request.
                URL url = null;
                try {
                    url = new URL("http://itunes.apple.com/search?term=" + artist + "+" + album + "&entity=album");
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    continue;
                }
                String xml = null;
                try {
                    //Create a new HTTP connection.
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.connect();
                    //Check if albumArt.xml already exists and delete it.
                    if (xmlFile.exists()) {
                        xmlFile.delete();
                    }
                    //Create the OuputStream that will be used to store the downloaded data into the file.
                    FileOutputStream fileOutput = new FileOutputStream(xmlFile);
                    //Create the InputStream that will read the data from the HTTP connection.
                    InputStream inputStream = urlConnection.getInputStream();
                    //Total size of target file.
                    int totalSize = urlConnection.getContentLength();
                    //Temp variable that stores the number of downloaded bytes.
                    int downloadedSize = 0;
                    //Create a buffer to store the downloaded bytes.
                    buffer = new byte[1024];
                    int bufferLength = 0;
                    //Now read through the buffer and write the contents to the file.
                    while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                    }
                    //Close the File Output Stream.
                    fileOutput.close();
                } catch (MalformedURLException e) {
                    //TODO Auto-generated method stub
                    continue;
                } catch (IOException e) {
                    // TODO Auto-generated method stub
                    continue;
                }
                //Load the XML file into a String variable for local use.
                String xmlAsString = null;
                try {
                    xmlAsString = FileUtils.readFileToString(xmlFile);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //Extract the albumArt parameter from the XML file.
                artworkURL = StringUtils.substringBetween(xmlAsString, "\"artworkUrl100\":\"", "\",");
                if (artworkURL == null) {
                    //Check and see if a lower resolution image available.
                    artworkURL = StringUtils.substringBetween(xmlAsString, "\"artworkUrl60\":\"", "\",");
                    if (artworkURL == null) {
                    //Can't do anything about that here.
                    } else {
                        //Replace "100x100" with "600x600" to retrieve larger album art images.
                        artworkURL = artworkURL.replace("100x100", "600x600");
                    }
                } else {
                    //Replace "100x100" with "600x600" to retrieve larger album art images.
                    artworkURL = artworkURL.replace("100x100", "600x600");
                }
                //If no URL has been found, there's no point in continuing.
                if (artworkURL != null) {
                    artworkBitmap = null;
                    artworkBitmap = mApp.getImageLoader().loadImageSync(artworkURL);
                    File artworkFile = new File(Environment.getExternalStorageDirectory() + "/artwork.jpg");
                    //Save the artwork.
                    try {
                        FileOutputStream out = new FileOutputStream(artworkFile);
                        artworkBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        Artwork artwork = null;
                        try {
                            artwork = ArtworkFactory.createArtworkFromFile(artworkFile);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            setArtworkAsFile(artworkFile, dataURIsList.get(i));
                            continue;
                        } catch (ArrayIndexOutOfBoundsException e) {
                            // TODO Auto-generated catch block
                            setArtworkAsFile(artworkFile, dataURIsList.get(i));
                            continue;
                        } catch (Exception e) {
                            e.printStackTrace();
                            setArtworkAsFile(artworkFile, dataURIsList.get(i));
                            continue;
                        } catch (Error e) {
                            e.printStackTrace();
                            setArtworkAsFile(artworkFile, dataURIsList.get(i));
                            continue;
                        }
                        if (artwork != null) {
                            try {
                                //Remove the current artwork field and recreate it.
                                tag.deleteArtworkField();
                                tag.addField(artwork);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                setArtworkAsFile(artworkFile, dataURIsList.get(i));
                                continue;
                            } catch (Error e) {
                                e.printStackTrace();
                                setArtworkAsFile(artworkFile, dataURIsList.get(i));
                                continue;
                            }
                            try {
                                audioFile.commit();
                            } catch (CannotWriteException e) {
                                // TODO Auto-generated catch block
                                setArtworkAsFile(artworkFile, dataURIsList.get(i));
                                continue;
                            } catch (Error e) {
                                e.printStackTrace();
                                setArtworkAsFile(artworkFile, dataURIsList.get(i));
                                continue;
                            }
                        }
                        //Delete the temporary files that we stored during the fetching process.
                        if (artworkFile.exists()) {
                            artworkFile.delete();
                        }
                        if (xmlFile.exists()) {
                            xmlFile.delete();
                        }
                        //Set the files to null to help clean up memory.
                        artworkBitmap = null;
                        audioFile = null;
                        tag = null;
                        xmlFile = null;
                        artworkFile = null;
                    }
                }
            }
        }
    }
    audioFile = null;
    file = null;
    return null;
}
Also used : CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) MalformedURLException(java.net.MalformedURLException) Artwork(org.jaudiotagger.tag.images.Artwork) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) DBAccessHelper(com.jams.music.player.DBHelpers.DBAccessHelper) InputStream(java.io.InputStream) 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) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) TagException(org.jaudiotagger.tag.TagException) FileOutputStream(java.io.FileOutputStream) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File)

Example 19 with ReadOnlyFileException

use of org.jaudiotagger.audio.exceptions.ReadOnlyFileException 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 20 with ReadOnlyFileException

use of org.jaudiotagger.audio.exceptions.ReadOnlyFileException 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

ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)22 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)21 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)21 IOException (java.io.IOException)20 TagException (org.jaudiotagger.tag.TagException)20 AudioFile (org.jaudiotagger.audio.AudioFile)17 File (java.io.File)14 Tag (org.jaudiotagger.tag.Tag)14 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)9 Cursor (android.database.Cursor)6 Paint (android.graphics.Paint)6 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)5 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)5 ContentValues (android.content.ContentValues)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