ExoPlayer resume on same position on rotate screen

Ali Akram picture Ali Akram · Mar 15, 2018 · Viewed 7.7k times · Source

I am using ExoPlayer in my activity,What i want is to smoothly play video in portrait and landscape mode.For this purpose what I am doing is in onpause I save the currentPlayerPosition and seek player to that position in onresume but while rotating it face a jerk and video is stopped for a while and played to the saved position.

My code is below please help me how i can smoothly switch the mode portrait and landscape.Thanks

     @Override
public void onPause() {

    super.onPause();

    if (mExoPlayerView != null && mExoPlayerView.getPlayer() != null) {
        mResumeWindow = mExoPlayerView.getPlayer().getCurrentWindowIndex();
        mResumePosition = Math.max(0, mExoPlayerView.getPlayer().getContentPosition());
        mExoPlayerView.getPlayer().release();
    }
}


@Override
public void onDestroy() {
    super.onDestroy();
    if (mExoPlayerView.getPlayer() != null)
        mExoPlayerView.getPlayer().release();
}


  @Override
public void onSaveInstanceState(Bundle outState) {

    outState.putInt(STATE_RESUME_WINDOW, mResumeWindow);
    outState.putLong(STATE_RESUME_POSITION, mResumePosition);
    outState.putBoolean(STATE_PLAYER_FULLSCREEN, mExoPlayerFullscreen);
    super.onSaveInstanceState(outState);
}



   @Override
protected void onResume() {

    super.onResume();

    if (mExoPlayerView == null) {

        mExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exoplayer);

        videoURL = getIntent().getStringExtra("url");
        postID = getIntent().getIntExtra("UserID", 0);

        String userAgent = Util.getUserAgent(Vid.this, getApplicationContext().getApplicationInfo().packageName);
        DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(Vid.this, null, httpDataSourceFactory);
        Uri daUri = Uri.parse(videoURL);

        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

        if (daUri.toString().startsWith("https://player.vimeo"))
            mVideoSource = new HlsMediaSource(daUri, dataSourceFactory, 1, null, null);
        else
            mVideoSource = new ExtractorMediaSource(daUri, dataSourceFactory, extractorsFactory, null, null);

        initExoPlayer();

    } else {
        resumeExoPlayer();
    }

}


   private void resumeExoPlayer() {

    boolean haveResumePosition = mResumeWindow != C.INDEX_UNSET;

    if (haveResumePosition) {
        hideKeyboard();
        hideProgress();
        mExoPlayerView.getPlayer().seekTo(mResumeWindow, mResumePosition);
    }
}

private void initExoPlayer() {
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    LoadControl loadControl = new DefaultLoadControl();
    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), trackSelector, loadControl);
    mExoPlayerView.setPlayer(player);

    boolean haveResumePosition = mResumeWindow != C.INDEX_UNSET;

    if (haveResumePosition) {
        hideKeyboard();
        hideProgress();
        mExoPlayerView.getPlayer().seekTo(mResumeWindow, mResumePosition);

    }

    mExoPlayerView.getPlayer().prepare(mVideoSource);
    mExoPlayerView.getPlayer().setPlayWhenReady(true);

    mExoPlayerView.getPlayer().addListener(new Player.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {

        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

            if (playbackState == ExoPlayer.STATE_ENDED) {
                hideProgress();
                mExoPlayerView.getPlayer().seekTo(0);
                mExoPlayerView.getPlayer().setPlayWhenReady(false);
            } else if (playbackState == ExoPlayer.STATE_BUFFERING) {
            } else if (playbackState == ExoPlayer.STATE_READY) {
                hideProgress();

                if (preferenceManager.getLoggedIn()) {
                    APIGetComment();
                }
            }
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {

        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
            hideProgress();
            finish();
        }

        @Override
        public void onPositionDiscontinuity(int reason) {

        }

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

        }

        @Override
        public void onSeekProcessed() {

        }
    });
}

Answer

Ali Akram picture Ali Akram · Mar 19, 2018

Finally, After wasting 2 days I found it. Simple add it in the manifest and will work on all android version ?

android:configChanges="orientation|screenSize|layoutDirection"

cheers!