Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer resume on same position on rotate screen

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() {

        }
    });
}
like image 968
Ali Akram Avatar asked Mar 15 '18 15:03

Ali Akram


2 Answers

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!

like image 71
Ali Akram Avatar answered Sep 27 '22 21:09

Ali Akram


No need of any additional coding, simply add this line

android:configChanges="keyboardHidden|orientation|screenSize"

in your AndroidManifest.xml's activity section.

like image 42
Anand Avatar answered Sep 27 '22 22:09

Anand