Exoplayer-在片段内旋转时保存和还原状态 [英] Exoplayer - Save and Restore State On Rotation Within Fragment
本文介绍了Exoplayer-在片段内旋转时保存和还原状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个托管SimpleExoplayer的片段.我想确保我能正确处理屏幕旋转.现在,播放器将重置为屏幕旋转的开始.我已经在onStart()和onResume()中实现了方法,所以我很好奇我还需要什么其他代码:
I have a fragment that hosts a SimpleExoplayer. I want to ensure I am handling screen rotation properly. Right now, the player resets to the beginning on screen rotation. I already have methods implement in onStart() and onResume(), so I am curious what additional code I would need:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_recipe_details_three, container,false);
............
mStepDescription.setText(step.getDescription());
mVideoURL = step.getVideoURL();
mSimpleExoPlayer = v.findViewById(R.id.exoplayer);
mExoPlayerPlaceholder = v.findViewById(R.id.exoplayer_placeholder);
if (mVideoURL == null || mVideoURL.isEmpty()){
mSimpleExoPlayer.setVisibility(View.GONE);
mExoPlayerPlaceholder.setVisibility(View.VISIBLE);
}
return v;
}
onStart:
@Override
public void onStart() {
super.onStart();
initializePlayer();
}
onPause:
@Override
public void onPause() {
super.onPause();
if (mExoPlayer!=null) {
mExoPlayer.release();
mExoPlayer = null;
}
}
初始化:
private void initializePlayer(){
// Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
//Initialize the player
mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
mSimpleExoPlayer.setPlayer(mExoPlayer);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "CloudinaryExoplayer"));
// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// This is the MediaSource representing the media to be played.
Uri videoUri = Uri.parse(mVideoURL);
MediaSource videoSource = new ExtractorMediaSource(videoUri,
dataSourceFactory, extractorsFactory, null, null);
// Prepare the player with the source.
mExoPlayer.prepare(videoSource);
}
推荐答案
您无需为此更改清单.以下代码应该可以工作
You shouldn't need to change the manifest for this. The following code should work
override fun onSaveInstanceState(outState: Bundle) {
outState.putLong(KEY_PLAYER_POSITION, exoPlayer.contentPosition)
outState.putBoolean(KEY_PLAYER_PLAY_WHEN_READY, exoPlayer.playWhenReady)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
savedInstanceState?.let {
exoPlayer.seekTo(it.getLong(KEY_PLAYER_POSITION))
exoPlayer.playWhenReady = it.getBoolean(KEY_PLAYER_PLAY_WHEN_READY)
}
}
这篇关于Exoplayer-在片段内旋转时保存和还原状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文