Javier Manzano
Javier Manzano

Reputation: 4831

How to get streaming bitrate with MediaPlayer?

I'm developing a stream video app in Android with MediaPlayer. The problem is that I need to show the current bitrate, but I haven't found any valid suggestions on how to do get it?

Here is how I'm setting the video url to play:

    mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(VIDEO_PATH);
        mediaPlayer.prepare();
        mediaPlayer.init();
    } catch (IOException e) {
        e.printStackTrace();
    }

I don't know if the only way to get that working is using ExoPlayer (which I've read it may be possible)

Any suggestions?

Thanks!

Upvotes: 3

Views: 1734

Answers (1)

Apparently you cannot do this with MediaPlayer but you can use MediaMetadataRetriever, which is available since API level 10, i.e., quite a while ago.

  int getBitRate(String url) {
        final MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        try {
            mmr.setDataSource(url, Collections.EMPTY_MAP);
            return Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
        } catch (NumberFormatException e) {
            return 0;
        } finally {
            mmr.release();
        }
    }

The disadvantage this can have is that you will make an extra HTTP request for getting the metadata (only an RTT if you are streaming from an URI; if you are reading from an file descriptor it could be more serious). Hopefully no big deal.

Upvotes: 3

Related Questions