Search code examples
androidandroid-mediaplayerandroid-videoviewmediacontroller

Android mediaplayer reinitializes on screen rotation


I am streaming/playing already downloaded video in the VideoView. When I rotate the screen all the objects are reinitialized so that the video is played from the beginning or if get current position and set the position on screen rotation the video is streamed again. As the video from back end is encrypted,it takes so much time to load it resulting in a very bad user experience. The following is what I've os far.

public class MainActivity extends AppCompatActivity {
    VideoView videoView;
    Context context;
    int stopPosition = 0;
    private static MediaController mediaController;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        videoView = (VideoView) findViewById(R.id.videoView1);
        if (savedInstanceState != null) {
            stopPosition = savedInstanceState.getInt("position");
            Log.d("", "savedInstanceState called" + stopPosition);
        }
        mediaController = new MediaController(context);
        mediaController.setAnchorView(videoView);
        String path = "android.resource://" + getPackageName() + "/" + R.raw.videotest;
        Uri uri = Uri.parse(path);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }


    @Override
    public void onPause() {
        Log.d("", "onPause called");
        super.onPause();
        videoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("", "onResume called" + stopPosition);
        videoView.seekTo(stopPosition);
        videoView.resume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        videoView.pause();
        outState.putInt("position", 100);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        stopPosition = savedInstanceState.getInt("position");
    }


}

When I used the following methods this problem is solved but some new one starts.

If I add android:configChanges="screenSize|orientation" to AndroidManifest.xml it works perfectly except onSaveInstanceState is not called. I've some works to do in it so I want it be called. Another method I used is that using Mediaplayer and SurfaceView and it works too except the mediaplayer not functioned correctly in some cases(Video size changes in each videos)

Basically what I would like to have is that in above code onRestoreInstanceState should be called and video shouldn't reinitialized(or play without a delay) in screen rotation


Solution

  • Found a solution, if we add android:configChanges="screenSize|orientation" to manifest.xml then only onConfigurationChanged override is called, activity is not recreated. As I had one VideoView and a ListView in the screen which has parent LinearLayout, I changed orientation to horizontal from verical in onConfigurationChanged.

    You have to dynamically set required views in Portrait or Landscape mode accordingly.