Reputation: 3
I am trying to set Exoplayer into Fragment, I passed video url in initializePlayer(String mediaUri)
video working well, but if I rotate the device, video restarted, I read several tutorials to solve this issue with no success to call seekTo()
sorry for my English
public RecipeStepsVideoPlayerFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_recipe_steps_video_player, container, false);
ButterKnife.bind(this, view);
Bundle bundle=getArguments();
if(bundle!=null){
videoUrl=bundle.getString(VIDEO_URL_KEY);
}
initializePlayer(videoUrl);
stepDescription.setText(bundle.getString(STEP_DESCRIPTION_KEY));
return view;
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
startAutoPlay = savedInstanceState.getBoolean(KEY_AUTO_PLAY);
startWindow = savedInstanceState.getInt(KEY_WINDOW);
startPosition = savedInstanceState.getLong(KEY_POSITION);
Toast.makeText(getContext(),startPosition.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
releasePlayer();
outState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
outState.putInt(KEY_WINDOW, startWindow);
outState.putLong(KEY_POSITION, startPosition);
}
public void initializePlayer(String mediaUri) {
if (mExoPlayer == null) {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
mPlayerView.setPlayer(mExoPlayer);
mediaDataSourceFactory = new DefaultDataSourceFactory(getContext(),
Util.getUserAgent(getContext(), "BakingX"),
(TransferListener<? super DataSource>) bandwidthMeter);
mediaSource = new ExtractorMediaSource
.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(mediaUri));
//my prolem I can not call seekTo
if (startPosition!=null) {
mExoPlayer.seekTo(startWindow,startPosition);
}
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(playWhenReady);
}
}
private void releasePlayer() {
if (mExoPlayer!= null) {
startPosition = mExoPlayer.getCurrentPosition();
startWindow = mExoPlayer.getCurrentWindowIndex();
playWhenReady = mExoPlayer.getPlayWhenReady();
mExoPlayer.release();
mExoPlayer = null;
}
}
private void updateStartPosition() {
if (mExoPlayer != null) {
startAutoPlay = mExoPlayer.getPlayWhenReady();
startWindow = mExoPlayer.getCurrentWindowIndex();
startPosition = Math.max(0, mExoPlayer.getContentPosition());
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer(videoUrl);
}
}
@Override
public void onResume() {
super.onResume();
if (Util.SDK_INT <= 23 || mExoPlayer == null) {
initializePlayer(videoUrl);
}
}
@Override
public void onPause() {
super.onPause();
updateStartPosition();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
// releasePlayer();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
@Override
public void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
my question exactly how I call seekTo()
after mobile rotation
Upvotes: 0
Views: 2529
Reputation: 8000
A couple of issues:
1. Remove playWhenReady
and update everywhere it to startAutoPlay
.
2. Move the code from onViewRestored
to onCreateView
.
3. Update the position after setting the media source.
public RecipeStepsVideoPlayerFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recipe_steps_video_player, container, false);
ButterKnife.bind(this, view);
Bundle bundle = getArguments();
if (bundle != null) {
videoUrl = bundle.getString(VIDEO_URL_KEY);
}
if (savedInstanceState != null) {
startAutoPlay = savedInstanceState.getBoolean(KEY_AUTO_PLAY);
startWindow = savedInstanceState.getInt(KEY_WINDOW);
startPosition = savedInstanceState.getLong(KEY_POSITION);
Toast.makeText(getContext(), startPosition.toString(), Toast.LENGTH_LONG).show();
}
initializePlayer(videoUrl);
stepDescription.setText(bundle.getString(STEP_DESCRIPTION_KEY));
return view;
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
releasePlayer();
outState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
outState.putInt(KEY_WINDOW, startWindow);
outState.putLong(KEY_POSITION, startPosition);
}
public void initializePlayer(String mediaUri) {
if (mExoPlayer == null) {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
mPlayerView.setPlayer(mExoPlayer);
mediaDataSourceFactory = new DefaultDataSourceFactory(getContext(),
Util.getUserAgent(getContext(), "BakingX"),
(TransferListener<? super DataSource>) bandwidthMeter);
mediaSource = new ExtractorMediaSource
.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(mediaUri));
mExoPlayer.prepare(mediaSource);
if (startPosition != null) {
mExoPlayer.seekTo(startWindow, startPosition);
}
mExoPlayer.setPlayWhenReady(startAutoPlay);
}
}
private void releasePlayer() {
if (mExoPlayer != null) {
startPosition = mExoPlayer.getCurrentPosition();
startWindow = mExoPlayer.getCurrentWindowIndex();
startAutoPlay = mExoPlayer.getPlayWhenReady();
mExoPlayer.release();
mExoPlayer = null;
}
}
private void updateStartPosition() {
if (mExoPlayer != null) {
startAutoPlay = mExoPlayer.getPlayWhenReady();
startWindow = mExoPlayer.getCurrentWindowIndex();
startPosition = Math.max(0, mExoPlayer.getContentPosition());
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer(videoUrl);
}
}
@Override
public void onResume() {
super.onResume();
if (Util.SDK_INT <= 23 || mExoPlayer == null) {
initializePlayer(videoUrl);
}
}
@Override
public void onPause() {
super.onPause();
updateStartPosition();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
// releasePlayer();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
@Override
public void onDestroy() {
super.onDestroy();
releasePlayer();
}
Upvotes: 1