androidandroid-studioandroid-mediacodecmediaextractor

int bitrate = mediaformat.getInteger(MediaFormat.KEY_BIT_RATE) is returning NullPointerException


I am new to mediaCodec and using MediaFormat for extracting the video information. I am using:

int width = format.getInteger(MediaFormat.KEY_WIDTH);
int height  = format.getInteger(MediaFormat.KEY_HEIGHT);
int bitrate = format.getInteger(MediaFormat.KEY_BIT_RATE);

Height and Width are OK, I am getting correct height and width but the bitrate part is throwing an NullPointerException.

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference


Solution

  • Fixed this issue using

    MediaMetadataRetriever class.

    MediaMetadataRetriever m = new MediaMetadataRetriever();
    m.setDataSource(filepath);
    int bitrate = Integer.valueOf(m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
    

    This worked