gpt4 book ai didi

android - ExoPlayer 在旋转屏幕上的相同位置恢复

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:40 26 4
gpt4 key购买 nike

我在我的 Activity 中使用 ExoPlayer,我想要的是在纵向和横向模式下流畅地播放视频。为此我正在做的是在 onpause 我保存 currentPlayerPosition 并寻找播放器onresume 中的那个位置,但在旋转时它会突然晃动,视频会停止一会儿并播放到保存的位置。

我的代码如下,请帮助我如何顺利切换纵向和横向模式。谢谢

     @Override
public void onPause() {

super.onPause();

if (mExoPlayerView != null && mExoPlayerView.getPlayer() != null) {
mResumeWindow = mExoPlayerView.getPlayer().getCurrentWindowIndex();
mResumePosition = Math.max(0, mExoPlayerView.getPlayer().getContentPosition());
mExoPlayerView.getPlayer().release();
}
}


@Override
public void onDestroy() {
super.onDestroy();
if (mExoPlayerView.getPlayer() != null)
mExoPlayerView.getPlayer().release();
}


@Override
public void onSaveInstanceState(Bundle outState) {

outState.putInt(STATE_RESUME_WINDOW, mResumeWindow);
outState.putLong(STATE_RESUME_POSITION, mResumePosition);
outState.putBoolean(STATE_PLAYER_FULLSCREEN, mExoPlayerFullscreen);
super.onSaveInstanceState(outState);
}



@Override
protected void onResume() {

super.onResume();

if (mExoPlayerView == null) {

mExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exoplayer);

videoURL = getIntent().getStringExtra("url");
postID = getIntent().getIntExtra("UserID", 0);

String userAgent = Util.getUserAgent(Vid.this, getApplicationContext().getApplicationInfo().packageName);
DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(Vid.this, null, httpDataSourceFactory);
Uri daUri = Uri.parse(videoURL);

ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

if (daUri.toString().startsWith("https://player.vimeo"))
mVideoSource = new HlsMediaSource(daUri, dataSourceFactory, 1, null, null);
else
mVideoSource = new ExtractorMediaSource(daUri, dataSourceFactory, extractorsFactory, null, null);

initExoPlayer();

} else {
resumeExoPlayer();
}

}


private void resumeExoPlayer() {

boolean haveResumePosition = mResumeWindow != C.INDEX_UNSET;

if (haveResumePosition) {
hideKeyboard();
hideProgress();
mExoPlayerView.getPlayer().seekTo(mResumeWindow, mResumePosition);
}
}

private void initExoPlayer() {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), trackSelector, loadControl);
mExoPlayerView.setPlayer(player);

boolean haveResumePosition = mResumeWindow != C.INDEX_UNSET;

if (haveResumePosition) {
hideKeyboard();
hideProgress();
mExoPlayerView.getPlayer().seekTo(mResumeWindow, mResumePosition);

}

mExoPlayerView.getPlayer().prepare(mVideoSource);
mExoPlayerView.getPlayer().setPlayWhenReady(true);

mExoPlayerView.getPlayer().addListener(new Player.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onLoadingChanged(boolean isLoading) {

}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

if (playbackState == ExoPlayer.STATE_ENDED) {
hideProgress();
mExoPlayerView.getPlayer().seekTo(0);
mExoPlayerView.getPlayer().setPlayWhenReady(false);
} else if (playbackState == ExoPlayer.STATE_BUFFERING) {
} else if (playbackState == ExoPlayer.STATE_READY) {
hideProgress();

if (preferenceManager.getLoggedIn()) {
APIGetComment();
}
}
}

@Override
public void onRepeatModeChanged(int repeatMode) {

}

@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

}

@Override
public void onPlayerError(ExoPlaybackException error) {
hideProgress();
finish();
}

@Override
public void onPositionDiscontinuity(int reason) {

}

@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

}

@Override
public void onSeekProcessed() {

}
});
}

最佳答案

终于,在浪费了 2 天后,我找到了它。只需将其添加到 list 中即可在所有 Android 版本上运行?

android:configChanges="orientation|screenSize|layoutDirection"

干杯!

关于android - ExoPlayer 在旋转屏幕上的相同位置恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303165/

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