use of com.frostwire.mp3.Mp3File in project MusicDNA by harjot-oberai.
the class EditLocalSongFragment method onResume.
@Override
public void onResume() {
super.onResume();
mp3File = null;
try {
File f = new File(HomeActivity.editSong.getPath());
mp3File = (MP3File) AudioFileIO.read(f);
} catch (IOException | CannotReadException | InvalidAudioFrameException | TagException | ReadOnlyFileException e) {
e.printStackTrace();
} catch (ClassCastException e) {
mp3File = null;
e.printStackTrace();
}
if (mp3File == null) {
Toast.makeText(ctx, "Error in loading the file", Toast.LENGTH_SHORT).show();
mCallback.onEditSongSave(false);
}
if (mp3File != null && !mp3File.hasID3v2Tag()) {
Toast.makeText(ctx, "No Tags Found", Toast.LENGTH_SHORT).show();
mCallback.onEditSongSave(false);
}
if (mp3File != null && mp3File.hasID3v2Tag()) {
titleText.setText(HomeActivity.editSong.getTitle());
artistText.setText(HomeActivity.editSong.getArtist());
albumText.setText(HomeActivity.editSong.getAlbum());
}
try {
tag = mp3File.getTag();
id3v1Tag = mp3File.getID3v1Tag();
id3v2Tag = mp3File.getID3v2Tag();
id3v24Tag = mp3File.getID3v2TagAsv24();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(ctx, "Error in finding tags", Toast.LENGTH_SHORT).show();
mCallback.onEditSongSave(false);
}
}
use of com.frostwire.mp3.Mp3File in project MusicDNA by harjot-oberai.
the class AudioFileWriter method write.
/**
* Write the tag (if not empty) present in the AudioFile in the associated
* File
*
* @param af The file we want to process
* @throws CannotWriteException if anything went wrong
*/
// TODO Creates temp file in same folder as the original file, this is safe
// but would impose a performance overhead if the original file is on a networked drive
public void write(AudioFile af) throws CannotWriteException {
logger.config("Started writing tag data for file:" + af.getFile().getName());
// Prechecks
precheckWrite(af);
//mp3's use a different mechanism to the other formats
if (af instanceof MP3File) {
af.commit();
return;
}
RandomAccessFile raf = null;
RandomAccessFile rafTemp = null;
File newFile;
File result;
// Create temporary File
try {
newFile = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
}// Files/Write Data set to Deny
catch (IOException ioe) {
if (ioe.getMessage().equals(FILE_NAME_TOO_LONG) && (af.getFile().getName().length() > FILE_NAME_TOO_LONG_SAFE_LIMIT)) {
try {
newFile = File.createTempFile(af.getFile().getName().substring(0, FILE_NAME_TOO_LONG_SAFE_LIMIT).replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
} catch (IOException ioe2) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()), ioe2);
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()));
}
} else {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()), ioe);
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_CREATE_TEMPORARY_FILE_IN_FOLDER.getMsg(af.getFile().getName(), af.getFile().getParentFile().getAbsolutePath()));
}
}
// Open temporary file and actual file for editing
try {
rafTemp = new RandomAccessFile(newFile, WRITE_MODE);
raf = new RandomAccessFile(af.getFile(), WRITE_MODE);
}// Folders/Append Data set to Deny
catch (IOException ioe) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath()), ioe);
// If we managed to open either file, delete it.
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe2) {
// Warn but assume has worked okay
logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile(), ioe.getMessage()), ioe2);
}
// rafTemp)
if (!newFile.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath()));
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(af.getFile().getAbsolutePath()));
}
// Write data to File
try {
raf.seek(0);
rafTemp.seek(0);
try {
if (this.modificationListener != null) {
this.modificationListener.fileWillBeModified(af, false);
}
writeTag(af.getTag(), raf, rafTemp);
if (this.modificationListener != null) {
this.modificationListener.fileModified(af, newFile);
}
} catch (ModifyVetoException veto) {
throw new CannotWriteException(veto);
}
} catch (Exception e) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage()), e);
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe) {
// Warn but assume has worked okay
logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe);
}
// file so we can just delete it.
if (!newFile.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getAbsolutePath()));
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE.getMsg(af.getFile(), e.getMessage()));
} finally {
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
} catch (IOException ioe) {
// Warn but assume has worked okay
logger.log(Level.WARNING, ErrorMessage.GENERAL_WRITE_PROBLEM_CLOSING_FILE_HANDLE.getMsg(af.getFile().getAbsolutePath(), ioe.getMessage()), ioe);
}
}
// Result held in this file
result = af.getFile();
// If the temporary file was used
if (newFile.length() > 0) {
// Rename Original File
// Can fail on Vista if have Special Permission 'Delete' set Deny
File originalFileBackup = new File(af.getFile().getAbsoluteFile().getParentFile().getPath(), AudioFile.getBaseFilename(af.getFile()) + ".old");
//If already exists modify the suffix
int count = 1;
while (originalFileBackup.exists()) {
originalFileBackup = new File(af.getFile().getAbsoluteFile().getParentFile().getPath(), AudioFile.getBaseFilename(af.getFile()) + ".old" + count);
count++;
}
boolean renameResult = Utils.rename(af.getFile(), originalFileBackup);
if (!renameResult) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getAbsolutePath(), originalFileBackup.getName()));
//Delete the temp file because write has failed
if (newFile != null) {
newFile.delete();
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(af.getFile().getPath(), originalFileBackup.getName()));
}
// Rename Temp File to Original File
renameResult = Utils.rename(newFile, af.getFile());
if (!renameResult) {
// New File doesnt exist
if (!newFile.exists()) {
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_NEW_FILE_DOESNT_EXIST.getMsg(newFile.getAbsolutePath()));
}
// Rename the backup back to the original
if (!originalFileBackup.renameTo(af.getFile())) {
// TODO now if this happens we are left with testfile.old
// instead of testfile.mp4
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_BACKUP_TO_ORIGINAL.getMsg(originalFileBackup.getAbsolutePath(), af.getFile().getName()));
}
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getAbsolutePath(), newFile.getName()));
} else {
// Rename was okay so we can now delete the backup of the
// original
boolean deleteResult = originalFileBackup.delete();
if (!deleteResult) {
// Not a disaster but can'timer delete the backup so make a
// warning
logger.warning(ErrorMessage.GENERAL_WRITE_WARNING_UNABLE_TO_DELETE_BACKUP_FILE.getMsg(originalFileBackup.getAbsolutePath()));
}
}
// Delete the temporary file if still exists
if (newFile.exists()) {
if (!newFile.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getPath()));
}
}
} else {
// Delete the temporary file that wasn'timer ever used
if (!newFile.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getPath()));
}
}
if (this.modificationListener != null) {
this.modificationListener.fileOperationFinished(result);
}
}
use of com.frostwire.mp3.Mp3File in project frostwire by frostwire.
the class SoundcloudDownload method setAlbumArt.
private static boolean setAlbumArt(SoundcloudSearchResult sr, byte[] cover, String inPath, String outPath) {
try {
Mp3File mp3 = new Mp3File(inPath);
ID3Wrapper newId3Wrapper = new ID3Wrapper(new ID3v1Tag(), new ID3v23Tag());
newId3Wrapper.setAlbum(sr.getUsername() + ": " + sr.getDisplayName() + " via SoundCloud.com");
newId3Wrapper.setArtist(sr.getUsername());
newId3Wrapper.setTitle(sr.getDisplayName());
newId3Wrapper.setAlbumImage(cover, "image/jpg");
newId3Wrapper.setUrl(sr.getDetailsUrl());
newId3Wrapper.getId3v2Tag().setPadding(true);
mp3.setId3v1Tag(newId3Wrapper.getId3v1Tag());
mp3.setId3v2Tag(newId3Wrapper.getId3v2Tag());
mp3.save(outPath);
return true;
} catch (Throwable e) {
LOG.error("Error setting art information for soundcloud download", e);
return false;
}
}
use of com.frostwire.mp3.Mp3File in project frostwire by frostwire.
the class MP3Parser method getValueSafe.
private String getValueSafe(String currentValue, AudioFile audioFile, String identifier) {
String value = currentValue;
if (value == null || value.length() == 0) {
if (audioFile instanceof MP3File && ((MP3File) audioFile).hasID3v2Tag()) {
try {
AbstractID3v2Tag v2tag = ((MP3File) audioFile).getID3v2Tag();
value = v2tag.getFirst(identifier);
} catch (Exception e) {
LOG.warn("Unable to get value for ID3v2 tag key: " + identifier);
}
}
}
return value;
}
use of com.frostwire.mp3.Mp3File in project frostwire by frostwire.
the class MP3Parser method getArtwork.
@Override
public BufferedImage getArtwork() {
BufferedImage image = super.getArtwork();
if (image == null) {
try {
MP3File mp3 = new MP3File(file.getAbsoluteFile());
if (mp3.hasID3v2Tag()) {
AbstractID3v2Tag tag = mp3.getID3v2Tag();
Artwork artwork = tag.getFirstArtwork();
if (artwork != null) {
byte[] data = artwork.getBinaryData();
image = imageFromData(data);
}
}
} catch (Throwable e) {
LOG.error("Unable to read cover art from mp3");
}
}
return image;
}
Aggregations