Search in sources :

Example 6 with TagException

use of org.jaudiotagger.tag.TagException in project Shuttle by timusus.

the class LyricsFragment method getLyrics.

public String getLyrics() {
    String lyrics = getActivity().getString(R.string.no_lyrics);
    String filePath = MusicUtils.getFilePath();
    if (filePath == null) {
        return lyrics;
    }
    if (filePath.startsWith("content://")) {
        String path = MusicUtils.getFilePath();
        if (path != null) {
            Query query = new Query.Builder().uri(Uri.parse(path)).projection(new String[] { MediaStore.Audio.Media.DATA }).build();
            Cursor cursor = SqlUtils.createQuery(getContext(), query);
            if (cursor != null) {
                int colIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(colIndex);
                }
                cursor.close();
            }
        }
    }
    File file = new File(filePath);
    if (file.exists()) {
        try {
            AudioFile audioFile = AudioFileIO.read(file);
            if (audioFile != null) {
                Tag tag = audioFile.getTag();
                if (tag != null) {
                    String tagLyrics = tag.getFirst(FieldKey.LYRICS);
                    if (tagLyrics != null && tagLyrics.length() != 0) {
                        lyrics = tagLyrics.replace("\r", "\n");
                    }
                }
            }
        } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | UnsupportedOperationException ignored) {
        }
    }
    return lyrics;
}
Also used : Query(com.simplecity.amp_library.model.Query) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Cursor(android.database.Cursor) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File)

Example 7 with TagException

use of org.jaudiotagger.tag.TagException 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);
    }
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) IOException(java.io.IOException) MP3File(org.jaudiotagger.audio.mp3.MP3File) File(java.io.File) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) FieldDataInvalidException(org.jaudiotagger.tag.FieldDataInvalidException) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) IOException(java.io.IOException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)

Example 8 with TagException

use of org.jaudiotagger.tag.TagException in project musiccabinet by hakko.

the class AudioTagService method updateMetadata.

public void updateMetadata(File file) {
    String extension = getExtension(file.getFilename()).toUpperCase();
    if (!ALLOWED_EXTENSIONS.contains(extension)) {
        return;
    }
    MetaData metaData = new MetaData();
    metaData.setMediaType(Mediatype.valueOf(extension));
    try {
        AudioFile audioFile = AudioFileIO.read(new java.io.File(file.getDirectory(), file.getFilename()));
        Tag tag = audioFile.getTag();
        if (tag != null) {
            metaData.setArtist(getTagField(tag, ARTIST));
            metaData.setArtistSort(getTagField(tag, ARTIST_SORT));
            metaData.setAlbumArtist(toAlbumArtist(tag));
            metaData.setAlbumArtistSort(getTagField(tag, ALBUM_ARTIST_SORT));
            metaData.setAlbum(toAlbum(getTagField(tag, ALBUM)));
            metaData.setTitle(getTagField(tag, TITLE));
            metaData.setYear(getTagField(tag, YEAR));
            metaData.setGenre(toGenre(getTagField(tag, GENRE)));
            metaData.setLyrics(getTagField(tag, LYRICS));
            metaData.setComposer(getTagField(tag, COMPOSER));
            metaData.setDiscNr(toFirstNumber(getTagField(tag, DISC_NO)));
            metaData.setDiscNrs(toShort(getTagField(tag, DISC_TOTAL)));
            metaData.setTrackNr(toFirstNumber(getTagField(tag, TRACK)));
            metaData.setTrackNrs(toShort(getTagField(tag, TRACK_TOTAL)));
            metaData.setCoverArtEmbedded(tag.getFirstArtwork() != null);
        }
        AudioHeader audioHeader = audioFile.getAudioHeader();
        if (audioHeader != null) {
            metaData.setVbr(audioHeader.isVariableBitRate());
            metaData.setBitrate((short) audioHeader.getBitRateAsNumber());
            metaData.setDuration((short) audioHeader.getTrackLength());
        }
        file.setMetaData(metaData);
    } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | RuntimeException e) {
        // AudioFileIO has been seen to throw NumberFormatException
        LOG.warn("Could not read metadata of file " + file.getFilename() + " from " + file.getDirectory(), e);
    }
}
Also used : AudioHeader(org.jaudiotagger.audio.AudioHeader) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) MetaData(com.github.hakko.musiccabinet.domain.model.library.MetaData) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) FlacTag(org.jaudiotagger.tag.flac.FlacTag) Tag(org.jaudiotagger.tag.Tag) AbstractID3v2Tag(org.jaudiotagger.tag.id3.AbstractID3v2Tag)

Example 9 with TagException

use of org.jaudiotagger.tag.TagException in project musiccabinet by hakko.

the class AudioTagService method getArtwork.

public Artwork getArtwork(java.io.File file) throws ApplicationException {
    Tag tag = null;
    try {
        AudioFile audioFile = AudioFileIO.read(file);
        tag = audioFile.getTag();
    } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | RuntimeException e) {
        throw new ApplicationException("Failed reading artwork from file " + file, e);
    }
    return tag == null ? null : tag.getFirstArtwork();
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) FlacTag(org.jaudiotagger.tag.flac.FlacTag) Tag(org.jaudiotagger.tag.Tag) AbstractID3v2Tag(org.jaudiotagger.tag.id3.AbstractID3v2Tag) IOException(java.io.IOException)

Example 10 with TagException

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

the class AsfFileReader method read.

/**
     * {@inheritDoc}
     */
@Override
public AudioFile read(final File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
    if (!f.canRead()) {
        throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_DO_NOT_HAVE_PERMISSION_TO_READ_FILE.getMsg(f.getAbsolutePath()));
    }
    InputStream stream = null;
    try {
        stream = new FullRequestInputStream(new BufferedInputStream(new FileInputStream(f)));
        final AsfHeader header = HEADER_READER.read(Utils.readGUID(stream), stream, 0);
        if (header == null) {
            throw new CannotReadException(ErrorMessage.ASF_HEADER_MISSING.getMsg(f.getAbsolutePath()));
        }
        if (header.getFileHeader() == null) {
            throw new CannotReadException(ErrorMessage.ASF_FILE_HEADER_MISSING.getMsg(f.getAbsolutePath()));
        }
        // Just log a warning because file seems to play okay
        if (header.getFileHeader().getFileSize().longValue() != f.length()) {
            logger.warning(ErrorMessage.ASF_FILE_HEADER_SIZE_DOES_NOT_MATCH_FILE_SIZE.getMsg(f.getAbsolutePath(), header.getFileHeader().getFileSize().longValue(), f.length()));
        }
        return new AudioFile(f, getAudioHeader(header), getTag(header));
    } catch (final CannotReadException e) {
        throw e;
    } catch (final Exception e) {
        throw new CannotReadException("\"" + f + "\" :" + e, e);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (final Exception ex) {
            LOGGER.severe("\"" + f + "\" :" + ex);
        }
    }
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) AsfHeader(org.jaudiotagger.audio.asf.data.AsfHeader) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)

Aggregations

TagException (org.jaudiotagger.tag.TagException)19 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)18 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)18 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)18 IOException (java.io.IOException)17 AudioFile (org.jaudiotagger.audio.AudioFile)14 File (java.io.File)11 Tag (org.jaudiotagger.tag.Tag)11 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)8 Cursor (android.database.Cursor)5 Paint (android.graphics.Paint)5 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)5 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)4 AlertDialog (android.app.AlertDialog)3 ContentValues (android.content.ContentValues)3 DialogInterface (android.content.DialogInterface)3 OnClickListener (android.content.DialogInterface.OnClickListener)3 CompoundButton (android.widget.CompoundButton)3 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)3 FileOutputStream (java.io.FileOutputStream)3