Example usage for android.media MediaMetadataRetriever MediaMetadataRetriever

List of usage examples for android.media MediaMetadataRetriever MediaMetadataRetriever

Introduction

In this page you can find the example usage for android.media MediaMetadataRetriever MediaMetadataRetriever.

Prototype

public MediaMetadataRetriever() 

Source Link

Usage

From source file:com.adityarathi.muo.services.AudioPlaybackService.java

/**
 * This method combines the current cursor with the specified playlist cursor.
 * @param newCursor/*  w  ww .  j a  v  a 2  s . com*/
 */
public void enqueuePlaylistCursor(Cursor newCursor) {

    String[] matrixCursorColumns = { DBAccessHelper.SONG_ARTIST, DBAccessHelper.SONG_ALBUM,
            DBAccessHelper.SONG_TITLE, DBAccessHelper.SONG_FILE_PATH, DBAccessHelper.SONG_DURATION,
            DBAccessHelper.SONG_GENRE, DBAccessHelper.SONG_ID, DBAccessHelper.SONG_ALBUM_ART_PATH,
            DBAccessHelper.SONG_SOURCE };

    //Create an empty matrix getCursor() with the specified columns.
    MatrixCursor mMatrixCursor = new MatrixCursor(matrixCursorColumns);

    //Make a copy of the old getCursor() and copy it's contents over to the matrix getCursor().
    Cursor tempCursor = getCursor();

    tempCursor.moveToFirst();
    MediaMetadataRetriever mMMDR = new MediaMetadataRetriever();
    for (int i = 0; i < tempCursor.getCount(); i++) {
        tempCursor.moveToPosition(i);

        //Check which type of getCursor() the service currently has.
        if (getCursor().getColumnIndex(DBAccessHelper.SONG_FILE_PATH) == -1) {

            //We'll have to manually extract the info from the audio file.
            /*            String songFilePath = tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH));
                                
                        try {
                           mMMDR.setDataSource(songFilePath);
                        } catch (Exception e) {
                           //Skip the song if there's a problem with reading it.
                           continue;
                        }*/

            String songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
            String songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
            String songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
            String songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);

            mMatrixCursor
                    .addRow(new Object[] { songArtist, songAlbum, songTitle, "", songDuration, songGenre });

        } else {

            mMatrixCursor.addRow(
                    new Object[] { tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ARTIST)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_TITLE)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_FILE_PATH)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_DURATION)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_GENRE)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ID)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_ALBUM_ART_PATH)),
                            tempCursor.getString(tempCursor.getColumnIndex(DBAccessHelper.SONG_SOURCE)) });

        }

    }

    tempCursor.close();

    //Copy the contents of the new getCursor() over to the MatrixCursor.
    if (newCursor.getCount() > 0) {

        String songArtist = "";
        String songAlbum = "";
        String songTitle = "";
        String filePath = "";
        String songDuration = "";
        for (int j = 0; j < newCursor.getCount(); j++) {
            /*            newCursor.moveToPosition(j);
                        filePath = newCursor.getString(newCursor.getColumnIndex(DBAccessHelper.PLAYLIST_SONG_FILE_PATH));
                                
                        try {
                           mMMDR.setDataSource(filePath);
                        } catch (Exception e) {
                           continue;
                        }*/

            //Get the metadata from the song file.
            songArtist = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
            songAlbum = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
            songTitle = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
            songDuration = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            String songGenre = mMMDR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);

            mMatrixCursor.addRow(
                    new Object[] { songArtist, songAlbum, songTitle, filePath, songDuration, songGenre });

        }

    }

    mEnqueuePerformed = true;
    newCursor.close();
    mCursor = (Cursor) mMatrixCursor;
    mMatrixCursor.close();

}

From source file:github.daneren2005.dsub.fragments.SubsonicFragment.java

public void displaySongInfo(final Entry song) {
    Integer duration = null;/*  w  w  w.j  a va  2  s . c  om*/
    Integer bitrate = null;
    String format = null;
    long size = 0;
    if (!song.isDirectory()) {
        try {
            DownloadFile downloadFile = new DownloadFile(context, song, false);
            File file = downloadFile.getCompleteFile();
            if (file.exists()) {
                MediaMetadataRetriever metadata = new MediaMetadataRetriever();
                metadata.setDataSource(file.getAbsolutePath());

                String tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                duration = Integer.parseInt((tmp != null) ? tmp : "0") / 1000;
                format = FileUtil.getExtension(file.getName());
                size = file.length();

                // If no duration try to read bitrate tag
                if (duration == null) {
                    tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
                    bitrate = Integer.parseInt((tmp != null) ? tmp : "0") / 1000;
                } else {
                    // Otherwise do a calculation for it
                    // Divide by 1000 so in kbps
                    bitrate = (int) (size / duration) / 1000 * 8;
                }

                if (Util.isOffline(context)) {
                    song.setGenre(metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
                    String year = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
                    song.setYear(Integer.parseInt((year != null) ? year : "0"));
                }
            }
        } catch (Exception e) {
            Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver");
        }
    }
    if (duration == null) {
        duration = song.getDuration();
    }

    List<Integer> headers = new ArrayList<>();
    List<String> details = new ArrayList<>();

    if (!song.isDirectory()) {
        headers.add(R.string.details_title);
        details.add(song.getTitle());
    }

    if (song instanceof PodcastEpisode) {
        headers.add(R.string.details_podcast);
        details.add(song.getArtist());

        headers.add(R.string.details_status);
        details.add(((PodcastEpisode) song).getStatus());
    } else if (!song.isVideo()) {
        if (song.getArtist() != null && !"".equals(song.getArtist())) {
            headers.add(R.string.details_artist);
            details.add(song.getArtist());
        }
        if (song.getAlbum() != null && !"".equals(song.getAlbum())) {
            headers.add(R.string.details_album);
            details.add(song.getAlbum());
        }
    }
    if (song.getTrack() != null && song.getTrack() != 0) {
        headers.add(R.string.details_track);
        details.add(Integer.toString(song.getTrack()));
    }
    if (song.getGenre() != null && !"".equals(song.getGenre())) {
        headers.add(R.string.details_genre);
        details.add(song.getGenre());
    }
    if (song.getYear() != null && song.getYear() != 0) {
        headers.add(R.string.details_year);
        details.add(Integer.toString(song.getYear()));
    }
    if (!Util.isOffline(context) && song.getSuffix() != null) {
        headers.add(R.string.details_server_format);
        details.add(song.getSuffix());

        if (song.getBitRate() != null && song.getBitRate() != 0) {
            headers.add(R.string.details_server_bitrate);
            details.add(song.getBitRate() + " kbps");
        }
    }
    if (format != null && !"".equals(format)) {
        headers.add(R.string.details_cached_format);
        details.add(format);
    }
    if (bitrate != null && bitrate != 0) {
        headers.add(R.string.details_cached_bitrate);
        details.add(bitrate + " kbps");
    }
    if (size != 0) {
        headers.add(R.string.details_size);
        details.add(Util.formatLocalizedBytes(size, context));
    }
    if (duration != null && duration != 0) {
        headers.add(R.string.details_length);
        details.add(Util.formatDuration(duration));
    }
    if (song.getBookmark() != null) {
        headers.add(R.string.details_bookmark_position);
        details.add(Util.formatDuration(song.getBookmark().getPosition() / 1000));
    }
    if (song.getRating() != 0) {
        headers.add(R.string.details_rating);
        details.add(song.getRating() + " stars");
    }

    headers.add(R.string.details_starred);
    details.add(Util.formatBoolean(context, song.isStarred()));

    if (song instanceof PodcastEpisode) {
        headers.add(R.string.details_description);
        details.add(song.getAlbum());
    }

    int title;
    if (song.isDirectory()) {
        title = R.string.details_title_album;
    } else if (song instanceof PodcastEpisode) {
        title = R.string.details_title_podcast;
    } else {
        title = R.string.details_title_song;
    }
    Util.showDetailsDialog(context, title, headers, details);
}

From source file:github.popeen.dsub.fragments.SubsonicFragment.java

public void displaySongInfo(final Entry song) {
    Integer duration = null;//from  w ww .jav  a2  s.co m
    Integer bitrate = null;
    String format = null;
    long size = 0;
    if (!song.isDirectory()) {
        try {
            DownloadFile downloadFile = new DownloadFile(context, song, false);
            File file = downloadFile.getCompleteFile();
            if (file.exists()) {
                MediaMetadataRetriever metadata = new MediaMetadataRetriever();
                metadata.setDataSource(file.getAbsolutePath());

                String tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                duration = Integer.parseInt((tmp != null) ? tmp : "0") / 1000;
                format = FileUtil.getExtension(file.getName());
                size = file.length();

                // If no duration try to read bitrate tag
                if (duration == null) {
                    tmp = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
                    bitrate = Integer.parseInt((tmp != null) ? tmp : "0") / 1000;
                } else {
                    // Otherwise do a calculation for it
                    // Divide by 1000 so in kbps
                    bitrate = (int) (size / duration) / 1000 * 8;
                }

                if (Util.isOffline(context)) {
                    song.setGenre(metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
                    String year = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
                    song.setYear(Integer.parseInt((year != null) ? year : "0"));
                }
            }
        } catch (Exception e) {
            Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver");
        }
    }
    if (duration == null) {
        duration = song.getDuration();
    }

    List<Integer> headers = new ArrayList<>();
    List<String> details = new ArrayList<>();

    if (!song.isDirectory()) {
        headers.add(R.string.details_title);
        details.add(song.getTitle());
    }

    if (song instanceof PodcastEpisode) {
        headers.add(R.string.details_podcast);
        details.add(song.getArtist());

        headers.add(R.string.details_status);
        details.add(((PodcastEpisode) song).getStatus());
    } else if (!song.isVideo()) {
        if (song.getArtist() != null && !"".equals(song.getArtist())) {
            headers.add(R.string.details_artist);
            details.add(song.getArtist());
        }
        if (song.getAlbum() != null && !"".equals(song.getAlbum())) {
            headers.add(R.string.details_album);
            details.add(song.getAlbum());
        }
    }
    if (song.getTrack() != null && song.getTrack() != 0) {
        headers.add(R.string.details_track);
        details.add(Integer.toString(song.getTrack()));
    }
    if (song.getGenre() != null && !"".equals(song.getGenre())) {
        headers.add(R.string.details_genre);
        details.add(song.getGenre());
    }
    if (song.getYear() != null && song.getYear() != 0) {
        headers.add(R.string.details_year);
        details.add(Integer.toString(song.getYear()));
    }
    if (!Util.isOffline(context) && song.getSuffix() != null) {
        headers.add(R.string.details_server_format);
        details.add(song.getSuffix());

        if (song.getBitRate() != null && song.getBitRate() != 0) {
            headers.add(R.string.details_server_bitrate);
            details.add(song.getBitRate() + " kbps");
        }
    }
    if (format != null && !"".equals(format)) {
        headers.add(R.string.details_cached_format);
        details.add(format);
    }
    if (bitrate != null && bitrate != 0) {
        headers.add(R.string.details_cached_bitrate);
        details.add(bitrate + " kbps");
    }
    if (size != 0) {
        headers.add(R.string.details_size);
        details.add(Util.formatLocalizedBytes(size, context));
    }
    if (duration != null && duration != 0) {
        headers.add(R.string.details_length);
        details.add(Util.formatDuration(duration));
    }
    if (song.getBookmark() != null) {
        headers.add(R.string.details_bookmark_position);
        details.add(Util.formatDuration(song.getBookmark().getPosition() / 1000));
    }
    if (song.getRating() != 0) {
        headers.add(R.string.details_rating);
        details.add(song.getRating() + " stars");
    }

    headers.add(R.string.details_starred);
    details.add(Util.formatBoolean(context, song.isStarred()));

    try {
        Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song);
        if (dates != null && dates[0] != null && dates[0] > 0) {
            headers.add(R.string.details_last_played);
            details.add(Util.formatDate((dates[1] != null && dates[1] > dates[0]) ? dates[1] : dates[0]));
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to get last played", e);
    }

    if (song instanceof PodcastEpisode) {
        headers.add(R.string.details_description);
        details.add(song.getAlbum());
    }

    int title;
    if (song.isDirectory()) {
        title = R.string.details_title_album;
    } else if (song instanceof PodcastEpisode) {
        title = R.string.details_title_podcast;
    } else {
        title = R.string.details_title_song;
    }
    Util.showDetailsDialog(context, title, headers, details);
}

From source file:com.aimfire.demo.CamcorderActivity.java

private void generateThumbAndPreview(String filePath) {
    if (BuildConfig.DEBUG)
        Log.d(TAG, "generateThumbAndPreview");

    String movieNameNoExt = MediaScanner.getMovieNameNoExt(filePath);
    String previewPath = MainConsts.MEDIA_3D_THUMB_PATH + movieNameNoExt + ".jpeg";
    String thumbPath = MainConsts.MEDIA_3D_THUMB_PATH + movieNameNoExt + ".jpg";

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    Bitmap bitmap = null;//w ww. ja  va 2s  . c  om

    try {
        FileInputStream inputStream = new FileInputStream(filePath);
        retriever.setDataSource(inputStream.getFD());
        inputStream.close();

        bitmap = retriever.getFrameAtTime(0L, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        retriever.release();

        if (bitmap != null) {
            FileOutputStream out = null;
            out = new FileOutputStream(previewPath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
            out.close();

            Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, MainConsts.THUMBNAIL_SIZE,
                    MainConsts.THUMBNAIL_SIZE);
            out = new FileOutputStream(thumbPath);
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.close();
        }
    } catch (Exception e) {
        if (BuildConfig.DEBUG)
            Log.e(TAG, "generateThumbAndPreview: exception" + e.getMessage());
        retriever.release();
        FirebaseCrash.report(e);
    }
}