Android Exoplayer保存状态

时间:2018-06-28 05:42:17

标签: java android android-layout exoplayer screen-rotation

嗨,我正在使用ExoPlayer在我的VideoActivity中显示视频内容。我正在使用三个变量来保存官方文档中定义的状态:playingPosition,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;
}
}

请看看我做错了什么。

1 个答案:

答案 0 :(得分:1)

首先,您需要准备播放器,然后才能寻找所需的位置。因此您的代码就像

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