万事须己运,他得非我贤。这篇文章主要讲述Android Exoplayer,在屏幕旋转时恢复视频相关的知识,希望能为你提供帮助。
我使用FragmentStatePagerAdapter来实例化一些片段,每个片段都包含SimpleExoPlayerView。在屏幕旋转,我希望视频自动恢复。我在saveInstantState中保存playbackPosition和currentWindow,并在onActivityCreated上恢复它们??。问题是当我旋转设备时,我正确恢复了playbackPosition,但是再次实例化了SimpleExoPlayerView并且视频没有自动恢复,似乎片段在屏幕旋转时再次被实例化
StepInfoActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_info);
...mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), recipeModel.getSteps(), recipes_json);
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
包含ExoPlayerView的片段
public class PlaceholderFragment extends Fragment {private static final String ARG_SECTION_NUMBER = "section_number";
SimpleExoPlayer player;
long playbackPosition;
int currentWindow;
boolean playWhenReady = true;
RecipeModel recipeModel;
int received_index;
@BindView(R.id.simple_exp_view)
SimpleExoPlayerView playerView;
@BindView(R.id.step_long_desc)
TextView step_desc;
@BindView(R.id.video_thumbnail)
ImageView videoThumbnail;
public PlaceholderFragment() {
}@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String recipes_json = getArguments().getString(DetailFragment.STEP_INFO_EXTRA);
received_index = getArguments().getInt(DetailFragment.STEP_INFO_POSITION, 0);
recipeModel = new Gson().fromJson(recipes_json, RecipeModel.class);
}@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("current_window", currentWindow);
savedInstanceState.putLong("play_back_pos", playbackPosition);
}@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_step_info, container, false);
ButterKnife.bind(this, rootView);
setPlayerVisibility();
StepModel stepModel = recipeModel.getSteps().get(getArguments().getInt(ARG_SECTION_NUMBER) - 1);
if (stepModel.getDescription() != null) {
step_desc.setText(stepModel.getDescription());
}return rootView;
}private void setPlayerVisibility() {
StepModel stepModel = recipeModel.getSteps().get(getArguments().getInt(ARG_SECTION_NUMBER) - 1);
if (stepModel.getVideoURL().isEmpty()) {
playerView.setVisibility(View.GONE);
} else if (!stepModel.getThumbnailURL().isEmpty()) {
videoThumbnail.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(stepModel.getThumbnailURL()).into(videoThumbnail);
} else {
playerView.setVisibility(View.VISIBLE);
}
}@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
playbackPosition = savedInstanceState.getLong("play_back_pos");
currentWindow = savedInstanceState.getInt("current_window");
}}@Override
public void onStart() {
super.onStart();
initializePlayer();
}@Override
public void onResume() {
super.onResume();
if ((Util.SDK_INT <
= 23 || player == null)) {
initializePlayer();
}
}@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <
= 23) {
releasePlayer();
}
}@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT >
23) {
releasePlayer();
}
}private void releasePlayer() {
if (player != null) {
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
playWhenReady = player.getPlayWhenReady();
player.release();
player = null;
}
}private void initializePlayer() {if (player == null) {player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(getContext()),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
Uri uri = Uri.parse(recipeModel.getSteps().get(getArguments().getInt(ARG_SECTION_NUMBER) - 1).getVideoURL());
//todo
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
videoThumbnail.setVisibility(View.GONE);
}
}private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("Baking_app")).
createMediaSource(uri);
}public static PlaceholderFragment newInstance(int sectionNumber, String recipeModel) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putString(DetailFragment.STEP_INFO_EXTRA, recipeModel);
fragment.setArguments(args);
return fragment;
}
}
FragmentStatePagerAdapter
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
List<
StepModel>
steps;
String recipeModel;
public SectionsPagerAdapter(FragmentManager fm, List<
StepModel>
steps, String recipeModel) {
super(fm);
this.steps = steps;
this.recipeModel = recipeModel;
}@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1, recipeModel);
}@Override
public int getCount() {
return steps.size();
}@Override
public CharSequence getPageTitle(int position) {
if (position == 0) return "intro";
else return "step " + position;
}@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
}
答案【Android Exoplayer,在屏幕旋转时恢复视频】我设法通过在清单中添加一行来让视频继续播放:
<
activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
this line ->
android:configChanges="keyboardHidden|orientation|screenSize"
android:icon="@drawable/appicon"
android:launchMode="singleTop">
推荐阅读
- Android rtl viewpager with rtl tablayout
- 如何在你的Cordova Android应用中添加对Samsung Multi Window和PopUp模式的支持
- 8个最佳的Magento扩展程序,将促进你的在线业务
- Cordova中的Android错误(请安装Android目标:” android-x”)
- 性能提升(通过为Magento2选择Headless浏览器)
- 编译的cordova android应用已成功构建,但未在android 4.2中启动
- Cordova(暂停和恢复事件未触发)
- 在Cordova中使用JavaScript创建签名板
- 如何从头开始在Windows中使用Cordova为Android创建混合移动应用程序