java – What is MediaMetadataRetriever.BitmapParams params in getFramesAtIndex()?-ThrowExceptions

Exception or error:

I am trying to retrieve all the frames from a video. For that I am using getFramesAtIndex() in MediaMetadataRetriever.

I am not sure about the third parameter in the function, which is MediaMetadataRetriever.BitmapParams params.
Can you explain the third parameter with an example. I tried to look in Android documentation, but there are no examples.

How to solve:

As per the docs for MediaMetadataRetriever#getFramesAtIndex:

This method retrieves a consecutive set of video frames starting at
the specified index. It should only be called after
setDataSource(Context, Uri). If the caller intends to retrieve more
than one consecutive video frames, this method is preferred over
getFrameAtIndex(int,
android.media.MediaMetadataRetriever.BitmapParams) for efficiency.
After the bitmaps are returned, you can query the actual parameters
that were used to create the bitmaps from the BitmapParams argument,
for instance to query the bitmap config used for the bitmaps with
BitmapParams#getActualConfig.

The bitmap params allows you to specify a preferred bitmap config to be returned.

Example:

val bitmapParams = MediaMetadataRetriever.BitmapParams()

// Set your preferred bitmap config e.g.
bitmapParams.preferredConfig = Bitmap.Config.RGB_565

val frameIndex = 0
val numFrames = 10
val frames = mediaMetadataRetriever.getFramesAtIndex(frameIndex, numFrames, bitmapParams)

// Can now query actual config of bitmap frames returned
bitmapParams.actualConfig

###

The method MediaMetadataRetriever.getFramesAtIndex() is overloaded. If you don’t have specific requirements for image quality, then you could use the two-arguments version of the method:

public List<Bitmap> getFramesAtIndex (int frameIndex, 
                int numFrames)

which requires no BitmapParams. This is choice provides the simplest code as possible. There are two reasons why you might want to use the three-arguments version of the method:

  • Specific image quality requirements
  • Efficiency, as the doc says:

If the caller intends to retrieve more than one consecutive video frames, this method is preferred over getFrameAtIndex(int, android.media.MediaMetadataRetriever.BitmapParams) for efficiency

To answer your question, the simplest usage of the three-arguments version is

getFramesAtIndex (myFrameIndex, 
                myNumFrames, 
                new MediaMetadataRetriever.BitmapParams());

because, if you look at the doc of BitmapParams, the default constructor

Creates a default BitmapParams object. By default, it uses Bitmap.Config#ARGB_8888 as the preferred bitmap config.

This is the preferrable choice in most cases, as discussed in the doc. As for non-default cases, suppose you want to specify the ALPHA_8 option instead of the default ARGB_8888. Then the code is as follows

getFramesAtIndex (myFrameIndex, 
                myNumFrames, 
                new MediaMetadataRetriever.BitmapParams()
                   .setPreferredConfig(Bitmap.Config.ALPHA_8));

Hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *