Android Exoplayer 保存状态

Android Exoplayer save state

您好,我正在使用 ExoPlayer 在我的 VideoActivity 中显示视频内容。我使用三个变量来保存官方文档中定义的状态:playbackPosition、currentWindow、playWhenReady 我在设备处于横向模式时全屏显示视频,所以我有 2 种布局,一种用于纵向模式,一种用于横向模式:

这是我的 VideoActivity 的代码,我已检查值是否已正确保存并已正确检索:

视频从我旋转设备时开始播放,而不是从保存的位置开始播放。

public class VideoActivity extends AppCompatActivity {

private SimpleExoPlayerView playerView;
private ExoPlayer player;

private int position;
private ArrayList<RecipeStep> steps;

private boolean playWhenReady;
private int currentWindow;
private long playbackPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if(savedInstanceState == null){
        playWhenReady = true;
        currentWindow = 0;
        playbackPosition = 0;
    }else {
        playWhenReady = savedInstanceState.getBoolean("playWhenReady");
        currentWindow = savedInstanceState.getInt("currentWindow");
        playbackPosition = savedInstanceState.getLong("playBackPosition");
    }

    if(getIntent().getExtras() != null) {
        position = getIntent().getIntExtra("position", 0);
        steps = getIntent().getParcelableArrayListExtra("steps");
    }

    playerView = findViewById(R.id.video_view);

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        setStepTexts(position);

}

private void setStepTexts(int pos){
    ((TextView) findViewById(R.id.step_short_description)).setText(steps.get(pos-1).getShort_description());
    ((TextView) findViewById(R.id.step_description)).setText(steps.get(pos-1).getDescription());
}

private void initializePayer(){
    player = ExoPlayerFactory.newSimpleInstance(
            new DefaultRenderersFactory(this),
            new DefaultTrackSelector(), new DefaultLoadControl());

    playerView.setPlayer(player);
    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);

    passVideoUri(position);

}

private void passVideoUri(int pos){
    Uri uri = Uri.parse(steps.get(pos-1).getVideo_url());
    MediaSource mediaSource = buildMediaSource(uri);
    player.prepare(mediaSource, true, false);
}

private MediaSource buildMediaSource(Uri uri){
    return new ExtractorMediaSource.Factory(
            new DefaultHttpDataSourceFactory("exoplayer-codelab")).createMediaSource(uri);
}

@Override
protected void onStart() {
    super.onStart();
    initializePayer();
}

@Override
protected void onStop() {
    super.onStop();
    releasePlayer();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    playbackPosition = player.getCurrentPosition();
    currentWindow = player.getCurrentWindowIndex();
    playWhenReady = player.getPlayWhenReady();

    outState.putBoolean("playWhenReady", playWhenReady);
    outState.putInt("currentWindow", currentWindow);
    outState.putLong("playBackPosition", playbackPosition);
    super.onSaveInstanceState(outState);
}

public void previous(View view){
    if(position > 1) {
        position--;
        setStepTexts(position);
        passVideoUri(position);
    }
}

public void next(View view){
    if(position < steps.size()) {
        position++;
        setStepTexts(position);
        passVideoUri(position);
    }
}

private void releasePlayer(){
    playbackPosition = player.getCurrentPosition();
    currentWindow = player.getCurrentWindowIndex();
    playWhenReady = player.getPlayWhenReady();
    player.release();
    player = null;
}
}

请看看我做错了什么。

首先你需要准备好播放器,然后你就可以寻找到想要的地方了。所以你的代码就像

passVideoUri(position); 
playerView.setPlayer(player); 
player.setPlayWhenReady(playWhenReady); 
player.seekTo(currentWindow, playbackPosition);