Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Exoplayer保存状态_Java_Android_Android Layout_Exoplayer_Screen Rotation - Fatal编程技术网

Java Android Exoplayer保存状态

Java Android Exoplayer保存状态,java,android,android-layout,exoplayer,screen-rotation,Java,Android,Android Layout,Exoplayer,Screen Rotation,您好,我正在使用ExoPlayer在我的视频活动中显示视频内容。我使用三个变量来保存官方文档中定义的状态:playbackPosition、currentWindow、playWhenReady 当设备处于横向模式时,我将全屏显示视频,因此我有两种布局,一种用于纵向模式,另一种用于横向模式: 这是我的VideoActivity的代码,我已检查值是否正确保存和检索: 视频从我旋转设备时开始,而不是从保存的位置开始 public class VideoActivity extends AppComp

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

这是我的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;
}
}
公共类VideoActivity扩展了AppCompative活动{
私人SimpleExoPlayerView-playerView;
私人玩家;
私人职位;
私有数组列表步骤;
私有布尔playWhenReady;
私有窗口;
私人长后卫位置;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_视频);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
如果(savedInstanceState==null){
playWhenReady=true;
currentWindow=0;
后卫位置=0;
}否则{
playwhenrady=savedInstanceState.getBoolean(“playwhenrady”);
currentWindow=savedInstanceState.getInt(“currentWindow”);
playbackPosition=savedInstanceState.getLong(“playbackPosition”);
}
if(getIntent().getExtras()!=null){
位置=getIntent().getIntExtra(“位置”,0);
步骤=getIntent().getParcelableArrayListExtra(“步骤”);
}
playerView=findviewbyd(R.id.video\u视图);
if(getResources().getConfiguration().orientation==Configuration.orientation\u纵向)
设置步骤(位置);
}
私有无效设置步骤文本(int pos){
((TextView)findViewById(R.id.step_short_description)).setText(steps.get(pos-1.getShort_description());
((TextView)findviewbyd(R.id.step_description)).setText(steps.get(pos-1.getDescription());
}
私有void initializePayer(){
player=ExoPlayerFactory.newSimpleInstance(
新建DefaultRendersFactory(此),
新建DefaultTrackSelector(),新建DefaultLoadControl());
playerView.setPlayer(player);
player.setplaywhenredy(playwhenredy);
player.seekTo(当前窗口,播放位置);
passVideoUri(位置);
}
私有void passVideoUri(int-pos){
Uri=Uri.parse(steps.get(pos-1.getVideo_url());
MediaSource=buildMediaSource(uri);
player.prepare(mediaSource,true,false);
}
私有媒体源buildMediaSource(Uri){
返回新的ExtractOrmDiaSource.Factory(
新的DefaultHttpDataSourceFactory(“exoplayer代码实验室”).createMediaSource(uri);
}
@凌驾
受保护的void onStart(){
super.onStart();
initializePayer();
}
@凌驾
受保护的void onStop(){
super.onStop();
释放玩家();
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
playbackPosition=player.getCurrentPosition();
currentWindow=player.getCurrentWindowIndex();
playwhenredy=player.getplaywhenredy();
outState.putBoolean(“playwhenredy”,playwhenredy);
outState.putInt(“currentWindow”,currentWindow);
普特隆(后场位置),后场位置;
super.onSaveInstanceState(超出状态);
}
公共作废上一页(视图){
如果(位置>1){
位置--;
设置步骤(位置);
passVideoUri(位置);
}
}
公共作废下一页(视图){
如果(位置

请看一看,看看我做错了什么。

首先你需要准备好球员,然后你可以寻找到想要的地方。所以你的代码应该是

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

首先你需要准备好球员,然后你可以寻找到想要的地方。所以你的代码应该是

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

尝试此链接我有代码,但它总是在landscap模式下播放视频尝试序列'passVideoUri(位置);playerView.setPlayer(player);player.setplaywhenredy(playwhenredy);播放器。seekTo(当前窗口,播放位置);'感谢@PankajKumar它起作用了:)@SahilShokeen作为回答添加了此链接我有代码,但它总是在landscap模式下播放视频尝试序列“passVideoUri(position)”;playerView.setPlayer(player);player.setplaywhenredy(playwhenredy);播放器。seekTo(当前窗口,播放位置);'“谢谢@PankajKumar它成功了:)@SahilShokeen补充道