How to keep decoding alive during screen orientation?

梦想的初衷 提交于 2019-12-11 03:36:59

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/19236498/how-to-keep-decoding-alive-during-screen-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!