6

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();
    }
}
Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
user3876897
  • 289
  • 1
  • 6
  • this problem is happening because when orientation get changes it automatically refreshes your code i mean it call onCreate method thats why this thing is happening with your code please go through my answer hop it will work for you – Android is everything for me Aug 08 '14 at 12:44

2 Answers2

0

Add

android:configChanges="orientation|screenSize|keyboardHidden"

in your Manifest file inside the

<activity> tag

usr30911
  • 2,731
  • 7
  • 26
  • 56
0

When you rotate the screen onCreate method is called again.

https://i.stack.imgur.com/z8GHs.png

Add this flag to your manifest

android:configChanges="orientation"
KunamiPT
  • 112
  • 6