How to implement autoresize/autorotate of Android Player?

If you are programming an Android App with a VideoPlayer, sometimes you want that it looks more like the YouTube App. The user navigates to a video and the first screen is in portrait mode such that the player is on top and the description and other information is below the player. The intuitive behaviour of the user would be to rotate the device to landscape mode to see the video in fullscreen mode. During the rotation, the video should not be interrupted. If the user rotates the device back to portrait mode, he/she would see the previous screen again and also here the video should not be interrupted during the rotation. The main challenge here are: - Which player should be used? VideoView oder MediaPlayer? - How to define the layout? - How to change the surfaceview of the player? - How to catch the rotation event?
1 answer

How to implement autoresize/autorotate of Android Player?

To be able to change the View of the player depending on the rotation of the device we have to use the MediaPlayer-Class instead of the VideoView-Class.

So first you have to extend the SurfaceView and to catch the event when the size of the view is calculating. This event is called "OnMeasure" (From Android Documentary: OnMeasure - Called to determine the size requirements for this view and all of its children). So every time the view was changed and has to be drawn again or when the device rotates, this event will be called. This is the moment where we have to calculate the size of our surfaceView. Therefore we need to know the orientation of the device.

To be able to handle the orientation-changed-event we have to set a flag in the AndroidManifest class.
So for the activity, which will contain the mediaplayer, we have to set android:configChanges="screenSize|orientation". Now we can handle this event without a restart of our activity and pass the new orientation to our surfaceview.

public void onConfigurationChanged(Configuration _newConfig) {
if (mSurfaceView != null)

Now our surfaceview is able to change the size depending on the orientation. To add it to our activity-layout we can add it to the xml as followed:

LinearLayout ...
android:layout_gravity="center" /

So actually that's it. A possible implementation of the custom surfaceview is attached to this solution. The final step is to implement the mediaplayer, which uses our custom surfaceview.

The implementation of the mediaplayer is documented here:

And a really good example-project can be found in the platforms-examples which are part of the Android SDK.