gpt4 book ai didi

java - Exoplayer - 在 fragment 内旋转时保存和恢复状态

转载 作者:行者123 更新时间:2023-11-29 23:40:29 26 4
gpt4 key购买 nike

我有一个托管 SimpleExoplayer 的 fragment 。我想确保我正确处理屏幕旋转。现在,播放器会在屏幕旋转时重置到开头。我已经在 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;
}

开始时:

    @Override
public void onStart() {
super.onStart();
initializePlayer();
}

暂停:

    @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);
}

最佳答案

我使用屏幕方向和 Exoplayer 来正确处理它,而无需破解任何东西或绕过它。据我所知,每次我尝试某件事时都会遇到障碍。

我猜最后一个是 View 需要几毫秒才能再次显示视频,播放器也需要几毫秒才能 catch 播放(有趣的是我的播放器是静态的并且使用应用程序上下文进行初始化)。

最后,我最终添加了 orientation 标记来显示。

现在旋转屏幕时非常流畅

关于java - Exoplayer - 在 fragment 内旋转时保存和恢复状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908685/

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