ExoPlayer resume on same position on rotate screen

前端 未结 4 1446
佛祖请我去吃肉
佛祖请我去吃肉 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: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

    
    
    
      
    
        
    
        
    
      
    
    

    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

    
    

提交回复
热议问题