ExoPlayer resume on same position on rotate screen

前端 未结 4 1483
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 14:42

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 c

相关标签:
4条回答
  • 2021-02-05 14:51

    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!

    0 讨论(0)
  • 2021-02-05 14:52

    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>
    
    0 讨论(0)
  • 2021-02-05 14:59

    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.

      public class VideoPlayerActivity extends Activity {
    
      public static final String sURL_KEY = "STREAMING_URL_KEY";
      public static final String sTOAST_TEXT = "Unable to stream, no media found";
      static final String LOADING = "PLAYER_LOADING";
      static final String STOPPED = "PLAYER_STOPPED";
      static final String PAUSED = "PLAYER_PAUSED";
      static final String PLAYING = "PLAYER_PLAYING";
      static final String IDLE = "PLAYER_IDLE";
      private static final String TAG = "StreamMediaActivity";
      int orientation;
      private Uri streamUrl;
      private SimpleExoPlayer mPlayer;
      private PlayerView playerView;
      private ProgressBar progressBar;
      private String mPlayerStatus;
      private long mPlaybackPosition = 0L;
      private boolean mIsPlayWhenReady = true;
      private int mCurrentWindow = 0;
      private Display display;
      private String STATE_RESUME_WINDOW = "resumeWindow";
      private String STATE_RESUME_POSITION = "resumePosition";
      private String STATE_PLAYER_FULLSCREEN = "playerFullscreen";
      private boolean mExoPlayerFullscreen = false;
    
      @Override
      protected void onCreate(@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 class
        String urlString = getIntent().getStringExtra(sURL_KEY);
        if (urlString != null) {
          streamUrl = Uri.parse(urlString);
        } else {
          Toast.makeText(this, sTOAST_TEXT, Toast.LENGTH_LONG).show();
          finish();
        }
      }
    
      @Override
      protected void onStart() {
        super.onStart();
        initPlayer();
      }
    
      @Override
      protected void onResume() {
        super.onResume();
        if (mPlaybackPosition != 0L && mPlayer != null) {
          mPlayer.seekTo(mCurrentWindow, mPlaybackPosition);
        }
      }
    
      @Override
      protected void onStop() {
        super.onStop();
      }
    
      @Override protected void onPause() {
        super.onPause();
        releasePlayer();
      }
    
    
    
        private void initPlayer() {
            // 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.Factory dataSourceFactory =
                new DefaultDataSourceFactory(this, Util.getUserAgent(this, getApplicationInfo().name));
    
        MediaSource mediaSource =
            new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(
                streamUrl);
    
    
        // CREATE A NEW INSTANCE OF EXO PLAYER
        if (mPlayer == null) {
          mPlayer = new SimpleExoPlayer.Builder(this, new DefaultRenderersFactory(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(new Player.EventListener() {
          @Override
          public void onPlayerStateChanged(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;
            }
          }
    
          @Override
          public void onPlayerError(ExoPlaybackException error) {
            Toast.makeText(VideoPlayerActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            finish();
          }
        });
      }
    
      @Override protected void onSaveInstanceState(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);
      }
    
      public void fullScreen() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
      }
    
      private void releasePlayer() {
        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.ConstraintLayout xmlns: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">
    
      <FrameLayout
          android: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.PlayerView
            android: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" />
    
        <ProgressBar
            android: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 = new Intent(context, VideoPlayerActivity.class);
                            streamVideoIntent.putExtra(sURL_KEY, stringUrl);
                            context.startActivity(streamVideoIntent);
    

    STEP 4 : Lastly add activity to manifest

    <activity android:name=".ui.videoplayer.VideoPlayerActivity"
            android:configChanges="orientation|screenSize|layoutDirection"
            />
    
    0 讨论(0)
  • 2021-02-05 15:00

    No need of any additional coding, simply add this line

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    in your AndroidManifest.xml's activity section.

    0 讨论(0)
提交回复
热议问题