10

When I rotate my phone, my Activity restarts. I have a video view playing video, I rotate and the video restarts. Now I found adding this to my activity in the manifest fixed it

<activity android:name="Vforum" android:configChanges="orientation"></activity>

The problem now is the video controls aren't being redrawn until they disappear and come back, thus leaving either really long controls going from landscape to portrait mode or really short controls going from portrait to landscape. Once they disappear and then I tap to make them come back, then they are correctly sized. Is there a better method of doing this?

peterh
  • 11,875
  • 18
  • 85
  • 108
Ronnie
  • 11,138
  • 21
  • 78
  • 140

5 Answers5

27

add

android:configChanges="orientation"

to your Activity in AndroidManifest.xml.

  • solved my issue won't download progressive video again..

If your target API level 13 or higher, you must include the screenSize value in addition to the orientation value as described here. So your tag may look like

android:configChanges="orientation|screenSize"
Idham Perdameian
  • 2,199
  • 24
  • 34
Ashok
  • 601
  • 8
  • 11
3

Consider remembering video file position in Activity lifecycle events. When Activity is created you could obtain video position and play it from the moment it was restarted.

In your Activity class:

@Override
protected void onCreate(Bundle bundle){
   super.onCreate(bundle);
   int mPos=bundle.getInt("pos"); // get position, also check if value exists, refer to documentation for more info
}

@Override
protected void onSaveInstanceState (Bundle outState){
    outState.putInt("pos", myVideoView.getCurrentPosition()); // save it here
}
Amarnath
  • 1,091
  • 3
  • 19
  • 32
  • What lifecycle event is called when rotating? I quickly googled and came up short. I see `myVideoView.getCurrentPosition()` will save the current time, but when I am rotating and it is reseting, I am also seeing that the progressive download starts over too. Could you provide a simple example? Sudo code would even work for me, thanks! – Ronnie May 12 '11 at 19:59
  • When you rotate phone, current Activity instance is destroyed and new instance is created, so you could check bundle object for video position in onCreate(). Video position could be saved in onSaveInstanceState(Bundle). Refer to http://developer.android.com/reference/android/app/Activity.html –  May 12 '11 at 20:05
  • What I'm seeing is that the video starts from some time in the future. Maybe its the next frame, about 1 second along. But information is lost. Anyone know how to rotate without losing any video? – Lucy Jan 18 '14 at 14:28
1

Adding the configChanges attribute to your manifest means that you will handle config changes yourself. Override the onConfigurationChanged() method in your activity:

int lastOrientation = 0;

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

    // Checks if orientation changed
    if (lastOrientation != newConfig.orientation) {
        lastOrientation = newConfig.orientation;
        // redraw your controls here
    } 
}
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Ohhh ok gotcha! I'm only on my second week of java so bare with me....how do I redraw the controls now? I tried `mVideoView.refreshDrawableState();` and failed. I dont know what method redraws them. – Ronnie May 12 '11 at 20:11
  • You don't want to recreate whole view as this will kill the video player. You need to replace only parts that need to change (controls) and leave player intact. Basically you will: locate the parent view group (layout), get positions of views to be replaced (index), remove them, inflate new ones, add to same position: http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time – Peter Knego May 12 '11 at 21:59
  • I am using the standard MediaController though. I don't want any layout to change. Its the built in video controls that need to be redrawn. I posted a video of what is happening: http://www.youtube.com/watch?v=FgRythmUo3A – Ronnie May 12 '11 at 22:48
  • any idea after watching the video? – Ronnie May 13 '11 at 16:43
0

ConfigChanges is right, but it did work like this;

    <activity
        android:configChanges="orientation|screenSize" ... >

and also in the Activity Class

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (lastOrientation != newConfig.orientation) {
        lastOrientation = newConfig.orientation;
        // redraw your controls here
    } 
}
lomec
  • 1,356
  • 1
  • 11
  • 10
0

You Should try this. Go to your AndroidManifest.xml file then past it tag.

<activity android:name=".activity_name"
 android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
imnhasan
  • 93
  • 1
  • 2
  • 10
  • I asked this question 7 years ago so I am sure things have changed. Still, thank you for your response. – Ronnie Jul 29 '18 at 04:23