-Fixed activity pause and resume lifecycle.

This commit is contained in:
John Zhen M 2017-09-14 19:52:40 -07:00 committed by John Zhen Mo
parent 174d040ca3
commit f9eb2a1ee5
6 changed files with 45 additions and 43 deletions

View File

@ -55,7 +55,7 @@ import org.schabi.newpipe.util.ThemeHelper;
*
* @author mauriciocolli
*/
public class BackgroundPlayer extends Service {
public final class BackgroundPlayer extends Service {
private static final String TAG = "BackgroundPlayer";
private static final boolean DEBUG = BasePlayer.DEBUG;

View File

@ -171,7 +171,6 @@ public abstract class BasePlayer implements Player.EventListener,
public BasePlayer(Context context) {
this.context = context;
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
this.audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
this.broadcastReceiver = new BroadcastReceiver() {
@Override
@ -329,6 +328,12 @@ public abstract class BasePlayer implements Player.EventListener,
audioManager.abandonAudioFocus(this);
audioManager = null;
}
}
public void destroy() {
if (DEBUG) Log.d(TAG, "destroy() called");
destroyPlayer();
if (playQueue != null) {
playQueue.dispose();
playQueue = null;
@ -337,11 +342,7 @@ public abstract class BasePlayer implements Player.EventListener,
playbackManager.dispose();
playbackManager = null;
}
}
public void destroy() {
if (DEBUG) Log.d(TAG, "destroy() called");
destroyPlayer();
unregisterBroadcastReceiver();
simpleExoPlayer = null;
@ -557,7 +558,7 @@ public abstract class BasePlayer implements Player.EventListener,
if (isCurrentWindowCorrect && getCurrentState() == STATE_PLAYING) return;
// Check timeline is up-to-date and has window
if (playbackManager.size() != simpleExoPlayer.getCurrentTimeline().getWindowCount()) return;
if (playbackManager.expectedTimelineSize() != simpleExoPlayer.getCurrentTimeline().getWindowCount()) return;
if (simpleExoPlayer.getCurrentTimeline().getWindowCount() <= currentSourceIndex) return;
// Check if window is ready
@ -637,7 +638,8 @@ public abstract class BasePlayer implements Player.EventListener,
changeState(playWhenReady ? STATE_PLAYING : STATE_PAUSED);
break;
case Player.STATE_ENDED: // 4
if (isPrepared) {
// Ensure the current window is loaded
if (simpleExoPlayer.isCurrentWindowSeekable()) {
changeState(STATE_COMPLETED);
isPrepared = false;
}
@ -742,7 +744,10 @@ public abstract class BasePlayer implements Player.EventListener,
if (!isPlaying()) audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
else audioManager.abandonAudioFocus(this);
if (getCurrentState() == STATE_COMPLETED) playQueue.setIndex(0);
if (getCurrentState() == STATE_COMPLETED) {
if (playQueue.getIndex() == 0) simpleExoPlayer.seekToDefaultPosition();
else playQueue.setIndex(0);
}
simpleExoPlayer.setPlayWhenReady(!isPlaying());
}

View File

@ -20,7 +20,6 @@
package org.schabi.newpipe.player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
@ -55,11 +54,10 @@ import static org.schabi.newpipe.util.AnimationUtils.animateView;
*
* @author mauriciocolli
*/
public class MainVideoPlayer extends Activity {
public final class MainVideoPlayer extends Activity {
private static final String TAG = ".MainVideoPlayer";
private static final boolean DEBUG = BasePlayer.DEBUG;
private AudioManager audioManager;
private GestureDetector gestureDetector;
private boolean activityPaused;
@ -76,7 +74,6 @@ public class MainVideoPlayer extends Activity {
ThemeHelper.setTheme(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (getIntent() == null) {
Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show();
@ -111,6 +108,7 @@ public class MainVideoPlayer extends Activity {
if (DEBUG) Log.d(TAG, "onStop() called");
activityPaused = true;
if (playerImpl.getPlayer() != null) {
playerImpl.wasPlaying = playerImpl.getPlayer().getPlayWhenReady();
playerImpl.setRecovery(
playerImpl.getCurrentQueueIndex(),
(int) playerImpl.getPlayer().getCurrentPosition()
@ -126,8 +124,10 @@ public class MainVideoPlayer extends Activity {
if (activityPaused) {
playerImpl.initPlayer();
playerImpl.getPlayPauseButton().setImageResource(R.drawable.ic_play_arrow_white);
playerImpl.playQueue.init();
//playerImpl.play(false);
playerImpl.getPlayer().setPlayWhenReady(playerImpl.wasPlaying);
playerImpl.initPlayback(playerImpl, playerImpl.playQueue);
activityPaused = false;
}
}
@ -495,7 +495,7 @@ public class MainVideoPlayer extends Activity {
private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f;
private float currentBrightness = .5f;
private int currentVolume, maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
private int currentVolume, maxVolume = playerImpl.audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
private final float stepsVolume = 15, stepVolume = (float) Math.ceil(maxVolume / stepsVolume), minVolume = 0;
private final String brightnessUnicode = new String(Character.toChars(0x2600));
@ -530,10 +530,10 @@ public class MainVideoPlayer extends Activity {
if (e1.getX() > playerImpl.getRootView().getWidth() / 2) {
double floor = Math.floor(up ? stepVolume : -stepVolume);
currentVolume = (int) (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + floor);
currentVolume = (int) (playerImpl.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + floor);
if (currentVolume >= maxVolume) currentVolume = maxVolume;
if (currentVolume <= minVolume) currentVolume = (int) minVolume;
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
playerImpl.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
if (DEBUG) Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
playerImpl.getVolumeTextView().setText(volumeUnicode + " " + Math.round((((float) currentVolume) / maxVolume) * 100) + "%");

View File

@ -87,7 +87,7 @@ import static org.schabi.newpipe.util.AnimationUtils.animateView;
*
* @author mauriciocolli
*/
public class PopupVideoPlayer extends Service {
public final class PopupVideoPlayer extends Service {
private static final String TAG = ".PopupVideoPlayer";
private static final boolean DEBUG = BasePlayer.DEBUG;
private static final int SHUTDOWN_FLING_VELOCITY = 10000;

View File

@ -300,17 +300,12 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
@Override
public void onBlocked() {
if (DEBUG) Log.d(TAG, "onLoading() called");
if (!isProgressLoopRunning()) startProgressLoop();
super.onBlocked();
controlsVisibilityHandler.removeCallbacksAndMessages(null);
animateView(controlsRoot, false, 300);
showAndAnimateControl(-1, true);
playbackSeekBar.setEnabled(true);
playbackSeekBar.setProgress(0);
playbackSeekBar.setEnabled(false);
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
@ -323,12 +318,19 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
@Override
public void onPlaying() {
if (DEBUG) Log.d(TAG, "onPlaying() called");
if (!isProgressLoopRunning()) startProgressLoop();
super.onPlaying();
showAndAnimateControl(-1, true);
playbackSeekBar.setEnabled(true);
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
loadingPanel.setVisibility(View.GONE);
showControlsThenHide();
animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200);
animateView(endScreen, false, 0);
}
@Override
@ -353,24 +355,13 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
@Override
public void onCompleted() {
if (DEBUG) Log.d(TAG, "onCompleted() called");
if (isProgressLoopRunning()) stopProgressLoop();
super.onCompleted();
showControls(500);
animateView(endScreen, true, 800);
animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200);
loadingPanel.setVisibility(View.GONE);
playbackSeekBar.setMax((int) simpleExoPlayer.getDuration());
playbackSeekBar.setProgress(playbackSeekBar.getMax());
playbackSeekBar.setEnabled(false);
playbackEndTime.setText(getTimeString(playbackSeekBar.getMax()));
playbackCurrentTime.setText(playbackEndTime.getText());
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
animateView(surfaceForeground, true, 100);
}

View File

@ -45,6 +45,7 @@ public class PlaybackManager {
private CompositeDisposable disposables;
private boolean isBlocked;
private boolean hasReset;
public PlaybackManager(@NonNull final PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
@ -72,8 +73,8 @@ public class PlaybackManager {
return sourceToQueueIndex.indexOf(playQueue.getIndex());
}
public int size() {
return sourceToQueueIndex.size();
public int expectedTimelineSize() {
return sources.getSize();
}
public void dispose() {
@ -178,6 +179,11 @@ public class PlaybackManager {
private boolean tryUnblock() {
if (isPlayQueueReady() && isCurrentIndexLoaded() && isBlocked) {
if (hasReset) {
playbackListener.prepare(sources);
hasReset = false;
}
isBlocked = false;
playbackListener.unblock();
return true;
@ -249,7 +255,7 @@ public class PlaybackManager {
if (this.sourceToQueueIndex != null) this.sourceToQueueIndex.clear();
this.sources = new DynamicConcatenatingMediaSource();
playbackListener.prepare(this.sources);
this.hasReset = true;
}
/*//////////////////////////////////////////////////////////////////////////