androidandroid-studioandroid-videoviewmediacontroller

portrait to landscape Video viewand controller restart problam in android studio


I have a videoView that i control it by MediaController it work well but when user change there phone position rotate in portrait to landscope mode or (upside down in landscope to portrait)video view

it will restart in first of video again

for more understanding I have take a video pleas watch it see this video https://www.youtube.com/watch?v=AtlzTqtc7zg&feature=youtu.be

my xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />
</LinearLayout>

my MainActivity.class

package sirwansoft.safir.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView = findViewById(R.id.videoView);

        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video);

        //control media
        MediaController mediaController = new MediaController(this);
        //set view with controller
        videoView.setMediaController(mediaController);
        //set Controller to view
        mediaController.setAnchorView(videoView);
        videoView.start();

    }
}

Solution

  • Add this code to your AndroidManifest file:

    <activity
        android:name="YOUR_ACTIVITY_NAME"
        android:configChanges="screenSize|orientation|keyboardHidden"
    />