Search in sources :

Example 1 with Artwork

use of org.jaudiotagger.tag.images.Artwork in project JamsMusicPlayer by psaravan.

the class AsyncGetAlbumArtTask method doInBackground.

@Override
protected Integer doInBackground(String... params) {
    //First, we'll make a HTTP request to iTunes' servers with the album and artist name.
    if (params.length == 2) {
        artist = params[0];
        album = params[1];
        //Create duplicate strings that will be filtered out for the URL.
        urlArtist = artist;
        urlAlbum = album;
        //Remove any unacceptable characters.
        if (urlArtist.contains("#")) {
            urlArtist = urlArtist.replace("#", "");
        }
        if (urlArtist.contains("$")) {
            urlArtist = urlArtist.replace("$", "");
        }
        if (urlArtist.contains("@")) {
            urlArtist = urlArtist.replace("@", "");
        }
        if (urlAlbum.contains("#")) {
            urlAlbum = urlAlbum.replace("#", "");
        }
        if (urlAlbum.contains("$")) {
            urlAlbum = urlAlbum.replace("$", "");
        }
        if (urlAlbum.contains("@")) {
            urlAlbum = urlAlbum.replace("@", "");
        }
        //Replace any spaces in the artist and album fields with "%20".
        if (urlArtist.contains(" ")) {
            urlArtist = urlArtist.replace(" ", "%20");
        }
        if (urlAlbum.contains(" ")) {
            urlAlbum = urlAlbum.replace(" ", "%20");
        }
    }
    //Construct the url for the HTTP request.
    URL uri = null;
    try {
        uri = new URL("http://itunes.apple.com/search?term=" + urlArtist + "+" + urlAlbum + "&entity=album");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return 1;
    }
    try {
        //Create a new HTTP connection.
        HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
        urlConnection.connect();
        //Set the destination directory for the xml file.
        File SDCardRoot = Environment.getExternalStorageDirectory();
        file = new File(SDCardRoot, "albumArt.xml");
        //Create the OuputStream that will be used to store the downloaded data into the file.
        FileOutputStream fileOutput = new FileOutputStream(file);
        //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.
        byte[] 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
        e.printStackTrace();
        return 1;
    } catch (IOException e) {
        // TODO Auto-generated method stub
        e.printStackTrace();
        return 1;
    }
    //Create a File object that points to the downloaded file.
    File phpSource = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/albumArt.xml");
    String phpAsString = null;
    try {
        phpAsString = FileUtils.readFileToString(phpSource);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 1;
    }
    //Extract the albumArt parameter from the PHP response.
    artworkURL = StringUtils.substringBetween(phpAsString, "\"artworkUrl100\":\"", "\",");
    if (artworkURL == null) {
        //Check and see if a lower resolution image available.
        artworkURL = StringUtils.substringBetween(phpAsString, "\"artworkUrl60\":\"", "\",");
        if (artworkURL == null) {
            URL_RETRIEVED = false;
            return 1;
        } else {
            //Replace "100x100" with "600x600" to retrieve larger album art images.
            artworkURL = artworkURL.replace("100x100", "600x600");
            URL_RETRIEVED = true;
        }
    } else {
        //Replace "100x100" with "600x600" to retrieve larger album art images.
        artworkURL = artworkURL.replace("100x100", "600x600");
        URL_RETRIEVED = true;
    }
    //Replace any rogue apostrophes.
    if (album.contains("'")) {
        album = album.replace("'", "''");
    }
    if (artist.contains("'")) {
        artist = artist.replace("'", "''");
    }
    String selection = DBAccessHelper.SONG_ALBUM + "=" + "'" + album + "'" + " AND " + DBAccessHelper.SONG_ARTIST + "=" + "'" + artist + "'";
    String[] projection = { DBAccessHelper._ID, DBAccessHelper.SONG_FILE_PATH };
    Cursor cursor = mApp.getDBAccessHelper().getWritableDatabase().query(DBAccessHelper.MUSIC_LIBRARY_TABLE, projection, selection, null, null, null, null);
    if (cursor.getCount() != 0) {
        cursor.moveToFirst();
        dataURIsList.add(cursor.getString(1));
        while (cursor.moveToNext()) {
            dataURIsList.add(cursor.getString(1));
        }
    }
    cursor.close();
    if (URL_RETRIEVED == true) {
        artworkBitmap = mApp.getImageLoader().loadImageSync(artworkURL);
        File artworkFile = new File(Environment.getExternalStorageDirectory() + "/artwork.jpg");
        //Display the album art on the grid/listview so that the user knows that the download is complete.
        publishProgress();
        //Save the artwork.
        try {
            FileOutputStream out = new FileOutputStream(artworkFile);
            artworkBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        } finally {
            for (int i = 0; i < dataURIsList.size(); i++) {
                if (dataURIsList.get(i) != null) {
                    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;
                    try {
                        if (f != null) {
                            tag = f.getTag();
                        } else {
                            continue;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    Artwork artwork = null;
                    try {
                        artwork = ArtworkFactory.createArtworkFromFile(artworkFile);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        setArtworkAsFile(artworkFile, dataURIsList.get(i));
                        continue;
                    } catch (Error e) {
                        e.printStackTrace();
                        setArtworkAsFile(artworkFile, dataURIsList.get(i));
                        continue;
                    }
                    if (artwork != null) {
                        try {
                            tag.setField(artwork);
                        } catch (FieldDataInvalidException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            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;
                        }
                    }
                    try {
                        f.commit();
                    } catch (CannotWriteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        setArtworkAsFile(artworkFile, dataURIsList.get(i));
                        continue;
                    } catch (Error e) {
                        e.printStackTrace();
                        setArtworkAsFile(artworkFile, dataURIsList.get(i));
                        continue;
                    }
                    //Update the album art tag in Jams' database.
                    ContentValues values = new ContentValues();
                    String filePath = dataURIsList.get(i);
                    filePath = filePath.replace("'", "''");
                    String where = DBAccessHelper.SONG_FILE_PATH + "=" + "'" + filePath + "'";
                    values.put(DBAccessHelper.SONG_ALBUM_ART_PATH, "byte://" + dataURIsList.get(i));
                    mApp.getDBAccessHelper().getWritableDatabase().update(DBAccessHelper.MUSIC_LIBRARY_TABLE, values, where, null);
                } else {
                    continue;
                }
            }
            //Refresh the memory/disk cache for the ImageLoader instance.
            try {
                mApp.getImageLoader().clearMemoryCache();
                mApp.getImageLoader().clearDiscCache();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //Delete the temporary files once the artwork has been embedded.
            artworkFile.delete();
            file.delete();
        }
    }
    return 0;
}
Also used : CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) ContentValues(android.content.ContentValues) MalformedURLException(java.net.MalformedURLException) Artwork(org.jaudiotagger.tag.images.Artwork) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InputStream(java.io.InputStream) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Cursor(android.database.Cursor) URL(java.net.URL) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) FieldDataInvalidException(org.jaudiotagger.tag.FieldDataInvalidException) 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) AudioFile(org.jaudiotagger.audio.AudioFile) HttpURLConnection(java.net.HttpURLConnection) TagException(org.jaudiotagger.tag.TagException) FieldDataInvalidException(org.jaudiotagger.tag.FieldDataInvalidException) 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 2 with Artwork

use of org.jaudiotagger.tag.images.Artwork in project MusicDNA by harjot-oberai.

the class Mp4Tag method getArtworkList.

public List<Artwork> getArtworkList() {
    List<TagField> coverartList = get(Mp4FieldKey.ARTWORK);
    List<Artwork> artworkList = new ArrayList<Artwork>(coverartList.size());
    for (TagField next : coverartList) {
        Mp4TagCoverField mp4CoverArt = (Mp4TagCoverField) next;
        Artwork artwork = ArtworkFactory.getNew();
        artwork.setBinaryData(mp4CoverArt.getData());
        artwork.setMimeType(Mp4TagCoverField.getMimeTypeForImageType(mp4CoverArt.getFieldType()));
        artworkList.add(artwork);
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork) ArrayList(java.util.ArrayList)

Example 3 with Artwork

use of org.jaudiotagger.tag.images.Artwork in project MusicDNA by harjot-oberai.

the class VorbisCommentTag method getArtworkList.

/**
     *
     * @return list of artwork images
     */
public List<Artwork> getArtworkList() {
    List<Artwork> artworkList = new ArrayList<Artwork>(1);
    //Read Old Format
    if (getArtworkBinaryData() != null & getArtworkBinaryData().length > 0) {
        Artwork artwork = ArtworkFactory.getNew();
        artwork.setMimeType(getArtworkMimeType());
        artwork.setBinaryData(getArtworkBinaryData());
        artworkList.add(artwork);
    }
    //New Format (Supports Multiple Images)
    List<TagField> metadataBlockPics = this.get(VorbisCommentFieldKey.METADATA_BLOCK_PICTURE);
    for (TagField tagField : metadataBlockPics) {
        try {
            byte[] imageBinaryData = Base64Coder.decode(((TagTextField) tagField).getContent());
            MetadataBlockDataPicture coverArt = new MetadataBlockDataPicture(ByteBuffer.wrap(imageBinaryData));
            Artwork artwork = ArtworkFactory.createArtworkFromMetadataBlockDataPicture(coverArt);
            artworkList.add(artwork);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        } catch (InvalidFrameException ife) {
            throw new RuntimeException(ife);
        }
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MetadataBlockDataPicture(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)

Example 4 with Artwork

use of org.jaudiotagger.tag.images.Artwork in project MusicDNA by harjot-oberai.

the class ID3v23Tag method getArtworkList.

/**
     * {@inheritDoc}
     */
public List<Artwork> getArtworkList() {
    List<TagField> coverartList = getFields(FieldKey.COVER_ART);
    List<Artwork> artworkList = new ArrayList<Artwork>(coverartList.size());
    for (TagField next : coverartList) {
        FrameBodyAPIC coverArt = (FrameBodyAPIC) ((AbstractID3v2Frame) next).getBody();
        Artwork artwork = ArtworkFactory.getNew();
        artwork.setMimeType(coverArt.getMimeType());
        artwork.setPictureType(coverArt.getPictureType());
        if (coverArt.isImageUrl()) {
            artwork.setLinked(true);
            artwork.setImageUrl(coverArt.getImageUrl());
        } else {
            artwork.setBinaryData(coverArt.getImageData());
        }
        artworkList.add(artwork);
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork)

Example 5 with Artwork

use of org.jaudiotagger.tag.images.Artwork in project MusicDNA by harjot-oberai.

the class ID3v24Tag method getArtworkList.

public List<Artwork> getArtworkList() {
    List<TagField> coverartList = getFields(FieldKey.COVER_ART);
    List<Artwork> artworkList = new ArrayList<Artwork>(coverartList.size());
    for (TagField next : coverartList) {
        FrameBodyAPIC coverArt = (FrameBodyAPIC) ((AbstractID3v2Frame) next).getBody();
        Artwork artwork = ArtworkFactory.getNew();
        artwork.setMimeType(coverArt.getMimeType());
        artwork.setPictureType(coverArt.getPictureType());
        if (coverArt.isImageUrl()) {
            artwork.setLinked(true);
            artwork.setImageUrl(coverArt.getImageUrl());
        } else {
            artwork.setBinaryData(coverArt.getImageData());
        }
        artworkList.add(artwork);
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork)

Aggregations

Artwork (org.jaudiotagger.tag.images.Artwork)9 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 Cursor (android.database.Cursor)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 AudioFile (org.jaudiotagger.audio.AudioFile)2 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)2 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)2 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)2 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)2 MetadataBlockDataPicture (org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)2 Tag (org.jaudiotagger.tag.Tag)2 TagException (org.jaudiotagger.tag.TagException)2 ContentValues (android.content.ContentValues)1 DBAccessHelper (com.jams.music.player.DBHelpers.DBAccessHelper)1