Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid Android VideoView corruption when rotating back to portrait

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

like image 804
Grzegorz Adam Hankiewicz Avatar asked Jun 29 '11 17:06

Grzegorz Adam Hankiewicz


People also ask

What happens when we change the orientation from portrait to landscape in Android?

As with almost all smartphones, Android supports two screen orientations: portrait and landscape. When the screen orientation of an Android device is changed, the current activity being displayed is destroyed and re-created automatically to redraw its content in the new orientation.

How do you handle rotation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What happens when Android screen rotates?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.


1 Answers

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:)

like image 170
TouchBoarder Avatar answered Oct 21 '22 00:10

TouchBoarder