Skip to content Skip to sidebar Skip to footer

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 currentPlayerPosi

Solution 1:

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!

Solution 2:

No need of any additional coding, simply add this line

android:configChanges="keyboardHidden|orientation|screenSize"

in your AndroidManifest.xml's activity section.

Solution 3:

If you want the video to resume on orientation change, you can add this to your manifest android:configChanges="keyboardHidden|orientation|screenSize"

     <activity
     <activity
         android:name=".MainActivity"
         android:name=".MainActivity"
         android:label="@string/app_name"
         android:label="@string/app_name"
+            android:configChanges="keyboardHidden|orientation|screenSize"
         android:theme="@style/AppTheme.NoActionBar"
         android:theme="@style/AppTheme.NoActionBar"
         android:icon="@mipmap/ic_launcher_2">
         android:icon="@mipmap/ic_launcher_2">
         <intent-filter>
         <intent-filter>

Solution 4:

I also wasted quite a lot time in this. Take a look at it EXO PLAYER 2.11.2

    implementation 'com.google.android.exoplayer:exoplayer:2.11.2'

STEP - 1 Make an activity in which string url is passed as intent.

publicclassVideoPlayerActivityextendsActivity {

  publicstaticfinalStringsURL_KEY="STREAMING_URL_KEY";
  publicstaticfinalStringsTOAST_TEXT="Unable to stream, no media found";
  staticfinalStringLOADING="PLAYER_LOADING";
  staticfinalStringSTOPPED="PLAYER_STOPPED";
  staticfinalStringPAUSED="PLAYER_PAUSED";
  staticfinalStringPLAYING="PLAYER_PLAYING";
  staticfinalStringIDLE="PLAYER_IDLE";
  privatestaticfinalStringTAG="StreamMediaActivity";
  int orientation;
  private Uri streamUrl;
  private SimpleExoPlayer mPlayer;
  private PlayerView playerView;
  private ProgressBar progressBar;
  private String mPlayerStatus;
  privatelongmPlaybackPosition=0L;
  privatebooleanmIsPlayWhenReady=true;
  privateintmCurrentWindow=0;
  private Display display;
  privateStringSTATE_RESUME_WINDOW="resumeWindow";
  privateStringSTATE_RESUME_POSITION="resumePosition";
  privateStringSTATE_PLAYER_FULLSCREEN="playerFullscreen";
  privatebooleanmExoPlayerFullscreen=false;

  @OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fullScreen();
    display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    orientation = display.getRotation();
    setContentView(R.layout.activity_video_player);
    playerView = findViewById(R.id.player_view);
    progressBar = findViewById(R.id.progressBar_player);

// Pass a string uri to this classStringurlString= getIntent().getStringExtra(sURL_KEY);
    if (urlString != null) {
      streamUrl = Uri.parse(urlString);
    } else {
      Toast.makeText(this, sTOAST_TEXT, Toast.LENGTH_LONG).show();
      finish();
    }
  }

  @OverrideprotectedvoidonStart() {
    super.onStart();
    initPlayer();
  }

  @OverrideprotectedvoidonResume() {
    super.onResume();
    if (mPlaybackPosition != 0L && mPlayer != null) {
      mPlayer.seekTo(mCurrentWindow, mPlaybackPosition);
    }
  }

  @OverrideprotectedvoidonStop() {
    super.onStop();
  }

  @OverrideprotectedvoidonPause() {
    super.onPause();
    releasePlayer();
  }



    privatevoidinitPlayer() {
        // ESTABLISH THE DATA SOURCE FROM URL// here i'm playing local video file that's// why using the DefaultDataSourceFactory but you //may use DefaultHttpDataSourceFactory to stream //online videos 
        DataSource.FactorydataSourceFactory=newDefaultDataSourceFactory(this, Util.getUserAgent(this, getApplicationInfo().name));

    MediaSourcemediaSource=newProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(
            streamUrl);


    // CREATE A NEW INSTANCE OF EXO PLAYERif (mPlayer == null) {
      mPlayer = newSimpleExoPlayer.Builder(this, newDefaultRenderersFactory(this)).build();
      playerView.setPlayer(mPlayer);
      progressBar.setVisibility(View.VISIBLE);
    }
    mPlayer.setPlayWhenReady(mIsPlayWhenReady);
    mPlayer.seekTo(mCurrentWindow, mPlaybackPosition);

    // PREPARE MEDIA PLAYER
    mPlayer.prepare(mediaSource, true, false);
    mPlayer.addListener(newPlayer.EventListener() {
      @OverridepublicvoidonPlayerStateChanged(boolean playWhenReady, int playbackState) {
        switch (playbackState) {
          case Player.STATE_BUFFERING:
            mPlayerStatus = LOADING;
            runOnUiThread(() -> progressBar.setVisibility(View.VISIBLE));
            break;
          case Player.STATE_ENDED:
            mPlayerStatus = STOPPED;
            break;
          case Player.STATE_READY:
            mPlayerStatus = (playWhenReady) ? PLAYING : PAUSED;
            runOnUiThread(() -> progressBar.setVisibility(View.INVISIBLE));
            break;
          default:
            mPlayerStatus = IDLE;
            break;
        }
      }

      @OverridepublicvoidonPlayerError(ExoPlaybackException error) {
        Toast.makeText(VideoPlayerActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
        finish();
      }
    });
  }

  @OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
    mExoPlayerFullscreen = !mExoPlayerFullscreen;
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_RESUME_WINDOW, mCurrentWindow);
    outState.putLong(STATE_RESUME_POSITION, mPlaybackPosition);
    outState.putBoolean(STATE_PLAYER_FULLSCREEN, mExoPlayerFullscreen);
    super.onSaveInstanceState(outState);
  }

  publicvoidfullScreen() {
    ViewdecorView= getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
  }

  privatevoidreleasePlayer() {
    if (mPlayer != null) {
      mPlayer.stop();
      mPlaybackPosition = mPlayer.getCurrentPosition();
      mCurrentWindow = mPlayer.getCurrentWindowIndex();
      mIsPlayWhenReady = mPlayer.getPlayWhenReady();
      playerView.setPlayer(null);
      mPlayer.release();
      mPlayer = null;
    }
  }
}

Step 2 : Make the XML layout

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/black"><FrameLayoutandroid:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><com.google.android.exoplayer2.ui.PlayerViewandroid:id="@+id/player_view"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"android:keepScreenOn="true"app:use_controller="true"app:resize_mode="fit"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><ProgressBarandroid:id="@+id/progressBar_player"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center" /></FrameLayout></androidx.constraintlayout.widget.ConstraintLayout>

STEP 3: start VideoPlayerActivity using intent from another activity

Intent streamVideoIntent = newIntent(context, VideoPlayerActivity.class);
                        streamVideoIntent.putExtra(sURL_KEY, stringUrl);
                        context.startActivity(streamVideoIntent);

STEP 4 : Lastly add activity to manifest

<activityandroid:name=".ui.videoplayer.VideoPlayerActivity"android:configChanges="orientation|screenSize|layoutDirection"
        />

Post a Comment for "Exoplayer Resume On Same Position On Rotate Screen"