5

Possible Duplicate:
Android: Keep MediaPlayer running during Activity screen orientation update

Android's YouTube app has some features:

When the screen of the device is in portrait and you watch a video and change the orientation of the screen to landscape, the video is shown full screen and the list of related videos disappears. It continues playing without restarting or restreaming.

Portrait:

Landscape:

The problems are:

  • When I create my layout XML files as "layout" and "layout-land", my Activity is destroyed when going from portrait to landscape. I have to restream the video.

  • If I set Androidmanifest.xml to use android:configChanges="orientation", then the Activity is not destroyed, but the layout does not change.

How can I achieve this?

Community
  • 1
  • 1
tungdx
  • 111
  • 2
  • 7

2 Answers2

0

You need a Thread for the media player (well, you'll already have one). Your activity can reliably determine whether it is destroyed due to a configuration change, see my answer here: How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.)

So it's not bad that your Activity is re-created; all you need to do is re-attach the player to it.

Community
  • 1
  • 1
class stacker
  • 5,357
  • 2
  • 32
  • 65
  • I understand your idea. I do follow it but it has a problem, when i rotate device, layout change to layout-land and mediaplyer has continue running, but I can hear sound but i can't see video. How could i fix it? Here my code: https://docs.google.com/file/d/0B4mVLTWlnazzUmpVVndOZDJaTTA/edit – tungdx Jan 25 '13 at 12:04
  • I see you have successfully followed the schematic approach. However, you have not attached the MediaPlayer to your newly created Activity. You'll have to call `setSurface()` on the already existing `MediaPlayer` object after the context change. – class stacker Jan 25 '13 at 12:20
  • @tungdx I've taken a second look at your code. One might expect this to work already. Have the logs provided any insight? – class stacker Jan 25 '13 at 13:29
  • Could you show me how i can set new setSurface() on the already existing MediaPlayer? and my english is bad, I don't understand your question "Have the logs provided any insight?". – tungdx Jan 25 '13 at 15:44
  • @tungdx Can you update your question with the log output you get when you rotate the device and notify me when you have done it? – class stacker Jan 25 '13 at 16:14
  • This is my logcat from my application. Before rotate: http://i1080.photobucket.com/albums/j321/xuantunga2/before_rotate_zps28c659a1.png and after rotate: http://i1080.photobucket.com/albums/j321/xuantunga2/after_rotate_zps76146251.png . – tungdx Jan 26 '13 at 03:13
  • @tungdx Thank you for the logs. You can try two things. 1) Disable hardware acceleration in the player settings. 2) If you play a local file, it would be better to `getCurrentPosition()`, then `stop()` before the activity is destroyed, then create the surface when the activity is again created, then call `prepareAsync()`, `seekTo()` and `start()`. – class stacker Jan 26 '13 at 15:53
  • I play a video streaming so it's not good to call prepareAsync() again. Help me, please! – tungdx Jan 28 '13 at 04:56
  • @tungdx Have you tried to disable hardware acceleration in the media player (manually)? Which Android versions du you test this with? – class stacker Jan 28 '13 at 06:14
  • @ClassStacker can u pls help me i m facing same issue in mediaplayer http://stackoverflow.com/questions/32624025/mediaplayer-object-getting-null-after-orientation-change – Erum Sep 17 '15 at 07:05
0

Add android:configChanges="orientation" in you AndroidManifest file;

Add:

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

In your Video activity;

Joao Guilherme
  • 387
  • 2
  • 10