在活动的onPause()中我将false传递给player.setPlayerWhenReady(),我按下手机的主页按钮,因此onPause()的活动被调用但我在后台保留app,我不是从堆栈中删除, 20分钟我打开应用程序,所以onStart()的活动被调用,再次初始化exoplayer,我传递有效的视频网址,然后我传递player.setPlayerWhenReady(真),但exo播放器直接调用其回调中的onPlayerError()..但当我从堆栈关闭应用程序并再次打开应用程序exo播放器工作正常...请帮助解决此问题。
代码是这样的..
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;
}
}
我在活动的onDestroy()中调用此发布方法。
答案 0 :(得分:1)
问题可能是因为您在没有首先释放它的情况下再次初始化Exoplayer。 在onPause中释放播放器通常是一种很好的做法:
private void releasePlayer() {
if (player != null) {
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
playWhenReady = player.getPlayWhenReady();
player.release();
player = null;
}
}
然后在onStart中,您可以使用用户离开的参数初始化播放器。
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);
}
P.S:代码与您的无关。这只是一个例子。