I`m using FragmentStatePagerAdapter to instantiate some fragments, each fragment contains SimpleExoPlayerView. on screen rotation, i want the video to be resumed automatically. i save playbackPosition and currentWindow in saveInstantState and restore them back on onActivityCreated. the problem is when i rotate the device, i restore the playbackPosition correctly but the SimpleExoPlayerView is instantiated again and the video not resumed automatically it seems that the fragment is instantiated again on screen rotation
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);
Fragment containing 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);
}
}
答案 0 :(得分:-1)
我设法通过在清单上添加一行来让视频继续播放:
<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">