gpt4 book ai didi

java - Android Exoplayer 保存状态

转载 作者:行者123 更新时间:2023-12-02 02:04:03 28 4
gpt4 key购买 nike

您好,我正在使用 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);

关于java - Android Exoplayer 保存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075420/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com