use of org.jaudiotagger.audio.AudioFile 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;
}
}
use of org.jaudiotagger.audio.AudioFile in project JamsMusicPlayer by psaravan.
the class ID3sSongEditorDialog method getSongTags.
//This method loops through all the songs and saves their tags into ArrayLists.
public void getSongTags(String uri) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
File file = new File(uri);
AudioFile audioFile = AudioFileIO.read(file);
if (audioFile != null && audioFile.getTag() != null) {
titleEditText.setText(audioFile.getTag().getFirst(FieldKey.TITLE));
artistEditText.setText(audioFile.getTag().getFirst(FieldKey.ARTIST));
albumEditText.setText(audioFile.getTag().getFirst(FieldKey.ALBUM));
albumArtistEditText.setText(audioFile.getTag().getFirst(FieldKey.ALBUM_ARTIST));
genreEditText.setText(audioFile.getTag().getFirst(FieldKey.GENRE));
producerEditText.setText(audioFile.getTag().getFirst(FieldKey.PRODUCER));
yearEditText.setText(audioFile.getTag().getFirst(FieldKey.YEAR));
trackEditText.setText(audioFile.getTag().getFirst(FieldKey.TRACK));
trackTotalEditText.setText(audioFile.getTag().getFirst(FieldKey.TRACK_TOTAL));
commentsEditText.setText(audioFile.getTag().getFirst(FieldKey.COMMENT));
}
}
use of org.jaudiotagger.audio.AudioFile 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;
}
use of org.jaudiotagger.audio.AudioFile 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;
}
use of org.jaudiotagger.audio.AudioFile 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;
}
Aggregations