Example usage for android.media MediaMetadataRetriever METADATA_KEY_BITRATE

List of usage examples for android.media MediaMetadataRetriever METADATA_KEY_BITRATE

Introduction

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

Prototype

int METADATA_KEY_BITRATE

To view the source code for android.media MediaMetadataRetriever METADATA_KEY_BITRATE.

Click Source Link

Document

This key retrieves the average bitrate (in bits/sec), if available.

Usage

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

public void displaySongInfo(final Entry song) {
    Integer duration = null;//  w w  w .  j  a v  a  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  .j av  a  2s  . c  o 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);
}