How to keep decoding alive during screen orientati

2019-07-20 12:41发布

Folks,

I am using MediaCodec to decode a network stream. Method configure() on MediaCodec takes a SurfaceView object as a parameter, making it easy to decode the output directly into the surface view.

The problem comes in when the device orientation is changed. The old surface gets destroyed and a new surface is created. The surface that mediacodec is still holding becomes invalid.

I cannot destroy MediaCodec object and recreate a new one. This would result in waiting for a few more seconds until a new key video frame arrives.

Looking at all the methods that are available on MediaCodec, it appears the only way to change the surface is by calling configure(). Is it okay to call configure() multiple times on the MediaCodec object?

Or, perhaps there is a way to store the internal state of the MediaCodec object and transfer it to a new MediaCodec object.

I would appreciate it if you can guide me in the right direction. Thank you in advance for your help.

2条回答
我想做一个坏孩纸
2楼-- · 2019-07-20 13:05

There's now an example of this in Grafika. See the "Double decode" activity.

It works by directing the output of the video decoder to a SurfaceTexture instead of directly to a view. The SurfaceTexture is attached to a TextureView. When the views change because of device rotation, a new TextureView is created, and we drop in the old SurfaceTexture with the setSurfaceTexture() method.

If you leave the activity with "back" or "home", the decoders are shut down. This is useful to avoid tying up hardware codec resources when switching to other apps.

It currently doesn't shut the decoders down if you turn the screen off with the power key, which may or may not be desirable.

To make life interesting, it decodes two streams side-by-side.

查看更多
贼婆χ
3楼-- · 2019-07-20 13:19

Why can't you set your activity to handle device orientation in manifest to keep your activity alive?.. http://developer.android.com/guide/topics/manifest/activity-element.html#config

查看更多
登录 后发表回答