I wrote a code to play a video from my site.
It works correctly but when I rotate my phone the video restarts from the start.
How do I solve this?
public class ActivityVideoDetail extends Activity {
private VideoDetail videoDetail;
private TextView txtResult;
// -------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_video_detail);
if (savedInstanceState != null)
{
}
else {
ini();
}
}
// -------------------------
private void ini() {
videoDetail = (VideoDetail) getIntent().getExtras().getSerializable(VideoDetail.VIDEO_DETAIL);
txtResult = (TextView) findViewById(R.id.txtResult);
txtResult.setText(videoDetail.getVideoTitle());
Uri uri = Uri.parse(videoDetail.getVideoPath());
// Uri uri = Uri.parse("http://daily3gp.com/vids/747.3gp");
VideoView videoView = (VideoView) findViewById(R.id.videoPlayer);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.start();
}
}
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).
In Android, when device configuration changes (such as users rotate the screen orientation, change language, etc..), the system will restart the running Activity ( onDestroyed() is called, followed by onCreate() ) to apply the new configuration.
You can do this by setting the android:configChanges flag on your Activity in AndroidManifest. xml as shown below: This flag signals to the Android platform that you are going to manually handle orientation, screenSize and keyboard appearance/disappearance changes for this Activity.
You just have to go to the AndroidManifest.xml and inside or in your activities labels, you have to type this line of code as someone up there said:
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="ActivityMenu"
android:configChanges="orientation|screenSize">
</activity>
Hope it works for you
There are generally three ways to do this:
As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored from savedInstanceState. This is done by overriding onSaveInstanceState and checking the parameter of onCreate.
You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape") to in your manifest.
You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="screenOrientation" in the tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With