Skip to content Skip to sidebar Skip to footer

Mediacodec.configure Fails With Illegalstateexception For 1080p Videos

I've been using grafika's MoviePlayer and bigFlake's ExtractMpegFramesTest to implement a seeking and extract frame feature in our app. These work fine for most of our user, howeve

Solution 1:

Ok, here to answer my own question again:

  • Firstly, why I can't extract frame with ExtractMpegFramesTest after using MoviePlayer to display videos:

    It appears that some devices can't handle 2 instances of MediaCodec at the same time for high res videos (>720p in the case of the Galaxy S4 mini) so you have to properly release one instance of MediaCodec before starting the other one, something like this:

    publicvoidreleaseResources() {
        // release everything we grabbedif (mDecoder != null) {
            try {
                mDecoder.stop();
                mDecoder.release();
                mDecoder = null;
            } catch (Exception e) {
                Timber.d("releaseResources(): message %s cause %s", e.getMessage(),  e.getCause());
                e.printStackTrace();
            }
        }
        if (mExtractor != null) {
            try {
                mExtractor.release();
                mExtractor = null;
            } catch (Exception e) {
                Timber.d("releaseResources(): message %s cause %s", e.getMessage(),  e.getCause());
                e.printStackTrace();
            }
        }
    }
    
  • Secondly, why the MoviePlayer fails to configure for 1080p videos. In my case, since I start this activity from a media picker activity with a VideoView for preview which has a MediaPlayer.OnErrorListener() registered to detect faulty video. The problem is the onError() callback has some really weird behavior on the Galaxy S4 mini while it work perfectly fine on my development devices. My wild guess this MediaPlayer.OnErrorListener() leaves the MediaCodec in a bad state that cause a lot of headache later on.

    So the solution is to set a blank OnErrorListener() and properly release the VideoView before doing anything else with MediaCodec. Something like this:

    mVideoView.setOnErrorListener(newMediaPlayer.OnErrorListener() {
            @OverridepublicbooleanonError(MediaPlayer mp, int what, int extra) {
                returntrue;
            }
    });
    .....
    mVideoView.stopPlayback();
    doOtherThingWithMediaCodec();
    

This explain a lot of weird thing that happen on different devices. I hope this help someone else debug their app.

Post a Comment for "Mediacodec.configure Fails With Illegalstateexception For 1080p Videos"