我有一个托管 SimpleExoplayer 的片段。我想确保我正确处理屏幕旋转。现在,播放器重置到屏幕旋转的起点。我已经在 onStart() 和 onResume() 中实现了方法,所以我很好奇我需要哪些额外的代码:
@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);
}