Avoid Android VideoView corruption when rotating b

2019-01-09 02:01发布

I've managed to write a limited video player able to view a .3gp file from internet. The video will be shown centered full screen, maintaining the video aspect ratio. Also, rotations don't interrupt the video, which keeps playing without problems.

Everything seems fine, but... on my HTC Legend when you rotate back to portrait, the video is corrupted, and instead of showing full screen it is displayed at its native pixel size. But rotating again to landscape works and is shown perfectly. Any ideas why? Unfortunately I don't have more hardware to test this on and I've run out of ideas to test.

You can get the full example source code from https://github.com/gradha/Android-video-stream-rotation. Here are screen captures of me opening the application, rotating to landscape, touching the screen to display the video controls, then rotating back to portrait to see the corruption. video started ok in portrait

on landscape the video works fine too

but now going back to portrait always shows corruption

2条回答
三岁会撩人
2楼-- · 2019-01-09 02:08

Turns out my whole test case was wrong. Right up until the commit where I blame the easy videoview example on being wrong everything was according to the book. However I forgot the android:configChanges="orientation" line, and adding this line on top of the previously mentioned commit makes everything work without video corruption.

I'll be marking hsigmond's answer as valid for providing a test example I could compare to and find out the true problem. My whole working around this with custom orientation handlers and a subclass of the VideoView was wrong and incorrectly based on the question Android VideoView orientation change with buffered video. Not that that is wrong, I simply applied it incorrectly (plus other answers there also mentioned the missing android:configChanges).

查看更多
混吃等死
3楼-- · 2019-01-09 02:27

In the source code at https://github.com/gradha/Android-video-stream-rotation. you added the comment:

Since we specified in the AndroidManifest.xml that we want to handle our own orientation changes, we resize the screen in function of being portrait or landscape.

From the source code AndroidManifest.xml

android:configChanges="orientation|screenSize"

So, if you add this attribute to the activity element in the manifest, I would interpret that as the activity will handle all the orientation changes? not you?

From Android Developers

To declare that your activity handles a configuration change, edit the appropriate activity element in your manifest file to include the android:configChanges attribute... more

So you should not need to:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

I created a test project to check if this was the case:

Rotating Video Stream Example: https://github.com/TouchBoarder/RotatingVideoStream

My conclusion: I did not need to overide the "onConfigurationChanged" in the activity to display the video correct in both portrait and landscape, and the video keeps playing on rotation changes.

Feel free to improve and use the code:)

查看更多
登录 后发表回答