in onPause() of activity i am passing false to player.setPlayerWhenReady(), i press home button of phone so onPause() of activity got called but i keep app in background, i am not removing from stack, after around 20 mins i re open the app so onStart() of activity got called where initialize exoplayer again and i pass valid video url, then i pass player.setPlayerWhenReady(true),but exo player directly calls onPlayerError() in its callback.. but when i close the app from stack and again open the app exo player works fine...please help to resolve this issue.
code is like this..
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
simpleExoPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
Log.d(AppConstants.LOG_TAG_LOG, "initializePlayer: player instance in initializePlayer is ="+player);
simpleExoPlayerView.setPlayer(player);
Log.d(AppConstants.LOG_TAG_LOG, "initializePlayer: shouldAutoPlay is ="+shouldAutoPlay);
player.setPlayWhenReady(true);
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(url), mediaDataSourceFactory,
extractorsFactory, null, null);
durationSet = false;
player.prepare(mediaSource);
seekToValue = scSchedule.getSeekPosition();
if (seekToValue != null) {
player.seekTo(seekToValue);
} else {
player.seekTo(0);
seekToValue = 0L;
}
player.addListener(new Player.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_ENDED) {
Log.d(AppConstants.LOG_TAG_LOG,
"In FullScreenVideoActivity::onPlayerStateChanged() ");
// increment videoViewCount in Analytics table
AppUtils.getInstance().updateAnalytics(false,
-1, true);
// update video watched duration in Analytics table
int videoViewDuration = 0;
if (seekToValue != null) {
if (filmDuration > seekToValue) {
videoViewDuration = (int) (filmDuration - seekToValue);
AppUtils.getInstance().updateAnalytics(false,
videoViewDuration / 1000,
false);
}
}
isVideoEnded = true;
videoEnded();
finish();
} else if (playbackState == ExoPlayer.STATE_READY && !durationSet) {
filmDuration = (int) player.getDuration();
Log.d(AppConstants.LOG_TAG_LOG,
"In FullScreenVideoActivity::onPlayerStateChanged(), " +
"filmDuration = " + filmDuration);
durationSet = true;
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
Log.d(AppConstants.LOG_TAG_LOG,"onPlayerError() called");
}
@Override
public void onPositionDiscontinuity() {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
});
private void releasePlayer() {
if (player != null) {
shouldAutoPlay = player.getPlayWhenReady();
player.release();
player = null;
trackSelector = null;
}
}
i call this release method in onDestroy() of activity.
Problem might be because you are initialising Exoplayer again without releasing it first. It's usually a good practice to release the player in onPause as:
private void releasePlayer() {
if (player != null) {
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
playWhenReady = player.getPlayWhenReady();
player.release();
player = null;
}
}
And then in onStart, you can initialise the player with the parameters where the user left.
private void initializePlayer() {
if (player == null) {
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
Uri uri = Uri.parse(getString(R.string.media_url_mp3));
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
}
PS: Code is not related to yours. Its just an example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.