Skip to content

Commit c0d172b

Browse files
author
John Zhen M
committed
-Fixed activity pause and resume lifecycle.
1 parent 867eece commit c0d172b

File tree

6 files changed

+47
-43
lines changed

6 files changed

+47
-43
lines changed

app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*
5656
* @author mauriciocolli
5757
*/
58-
public class BackgroundPlayer extends Service {
58+
public final class BackgroundPlayer extends Service {
5959
private static final String TAG = "BackgroundPlayer";
6060
private static final boolean DEBUG = BasePlayer.DEBUG;
6161

app/src/main/java/org/schabi/newpipe/player/BasePlayer.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ public abstract class BasePlayer implements Player.EventListener,
171171
public BasePlayer(Context context) {
172172
this.context = context;
173173
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
174-
this.audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
175174

176175
this.broadcastReceiver = new BroadcastReceiver() {
177176
@Override
@@ -208,6 +207,8 @@ public void initPlayer() {
208207
if (DEBUG) Log.d(TAG, "initPlayer() called with: context = [" + context + "]");
209208
initExoPlayerCache();
210209

210+
this.audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
211+
211212
AdaptiveTrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
212213
DefaultTrackSelector defaultTrackSelector = new DefaultTrackSelector(trackSelectionFactory);
213214
DefaultLoadControl loadControl = new DefaultLoadControl();
@@ -325,6 +326,12 @@ public void destroyPlayer() {
325326
audioManager.abandonAudioFocus(this);
326327
audioManager = null;
327328
}
329+
}
330+
331+
public void destroy() {
332+
if (DEBUG) Log.d(TAG, "destroy() called");
333+
destroyPlayer();
334+
328335
if (playQueue != null) {
329336
playQueue.dispose();
330337
playQueue = null;
@@ -333,11 +340,7 @@ public void destroyPlayer() {
333340
playbackManager.dispose();
334341
playbackManager = null;
335342
}
336-
}
337343

338-
public void destroy() {
339-
if (DEBUG) Log.d(TAG, "destroy() called");
340-
destroyPlayer();
341344
unregisterBroadcastReceiver();
342345

343346
simpleExoPlayer = null;
@@ -553,7 +556,7 @@ private void refreshTimeline() {
553556
if (isCurrentWindowCorrect && getCurrentState() == STATE_PLAYING) return;
554557

555558
// Check timeline is up-to-date and has window
556-
if (playbackManager.size() != simpleExoPlayer.getCurrentTimeline().getWindowCount()) return;
559+
if (playbackManager.expectedTimelineSize() != simpleExoPlayer.getCurrentTimeline().getWindowCount()) return;
557560
if (simpleExoPlayer.getCurrentTimeline().getWindowCount() <= currentSourceIndex) return;
558561

559562
// Check if window is ready
@@ -633,7 +636,8 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
633636
changeState(playWhenReady ? STATE_PLAYING : STATE_PAUSED);
634637
break;
635638
case Player.STATE_ENDED: // 4
636-
if (isPrepared) {
639+
// Ensure the current window is loaded
640+
if (simpleExoPlayer.isCurrentWindowSeekable()) {
637641
changeState(STATE_COMPLETED);
638642
isPrepared = false;
639643
}
@@ -738,7 +742,10 @@ public void onVideoPlayPause() {
738742
if (!isPlaying()) audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
739743
else audioManager.abandonAudioFocus(this);
740744

741-
if (getCurrentState() == STATE_COMPLETED) playQueue.setIndex(0);
745+
if (getCurrentState() == STATE_COMPLETED) {
746+
if (playQueue.getIndex() == 0) simpleExoPlayer.seekToDefaultPosition();
747+
else playQueue.setIndex(0);
748+
}
742749
simpleExoPlayer.setPlayWhenReady(!isPlaying());
743750
}
744751

app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.schabi.newpipe.player;
2121

2222
import android.app.Activity;
23-
import android.content.Context;
2423
import android.content.Intent;
2524
import android.content.pm.ActivityInfo;
2625
import android.graphics.Color;
@@ -55,11 +54,10 @@
5554
*
5655
* @author mauriciocolli
5756
*/
58-
public class MainVideoPlayer extends Activity {
57+
public final class MainVideoPlayer extends Activity {
5958
private static final String TAG = ".MainVideoPlayer";
6059
private static final boolean DEBUG = BasePlayer.DEBUG;
6160

62-
private AudioManager audioManager;
6361
private GestureDetector gestureDetector;
6462

6563
private boolean activityPaused;
@@ -76,7 +74,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7674
ThemeHelper.setTheme(this);
7775
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK);
7876
setVolumeControlStream(AudioManager.STREAM_MUSIC);
79-
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
8077

8178
if (getIntent() == null) {
8279
Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show();
@@ -111,6 +108,7 @@ protected void onStop() {
111108
if (DEBUG) Log.d(TAG, "onStop() called");
112109
activityPaused = true;
113110
if (playerImpl.getPlayer() != null) {
111+
playerImpl.wasPlaying = playerImpl.getPlayer().getPlayWhenReady();
114112
playerImpl.setRecovery(
115113
playerImpl.getCurrentQueueIndex(),
116114
(int) playerImpl.getPlayer().getCurrentPosition()
@@ -126,8 +124,10 @@ protected void onResume() {
126124
if (activityPaused) {
127125
playerImpl.initPlayer();
128126
playerImpl.getPlayPauseButton().setImageResource(R.drawable.ic_play_arrow_white);
129-
playerImpl.playQueue.init();
130-
//playerImpl.play(false);
127+
128+
playerImpl.getPlayer().setPlayWhenReady(playerImpl.wasPlaying);
129+
playerImpl.initPlayback(playerImpl, playerImpl.playQueue);
130+
131131
activityPaused = false;
132132
}
133133
}
@@ -495,7 +495,7 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
495495
private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f;
496496
private float currentBrightness = .5f;
497497

498-
private int currentVolume, maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
498+
private int currentVolume, maxVolume = playerImpl.audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
499499
private final float stepsVolume = 15, stepVolume = (float) Math.ceil(maxVolume / stepsVolume), minVolume = 0;
500500

501501
private final String brightnessUnicode = new String(Character.toChars(0x2600));
@@ -530,10 +530,10 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
530530

531531
if (e1.getX() > playerImpl.getRootView().getWidth() / 2) {
532532
double floor = Math.floor(up ? stepVolume : -stepVolume);
533-
currentVolume = (int) (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + floor);
533+
currentVolume = (int) (playerImpl.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + floor);
534534
if (currentVolume >= maxVolume) currentVolume = maxVolume;
535535
if (currentVolume <= minVolume) currentVolume = (int) minVolume;
536-
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
536+
playerImpl.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
537537

538538
if (DEBUG) Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
539539
playerImpl.getVolumeTextView().setText(volumeUnicode + " " + Math.round((((float) currentVolume) / maxVolume) * 100) + "%");

app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
*
8888
* @author mauriciocolli
8989
*/
90-
public class PopupVideoPlayer extends Service {
90+
public final class PopupVideoPlayer extends Service {
9191
private static final String TAG = ".PopupVideoPlayer";
9292
private static final boolean DEBUG = BasePlayer.DEBUG;
9393

app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,12 @@ private void buildPlaybackSpeedMenu(PopupMenu popupMenu) {
300300

301301
@Override
302302
public void onBlocked() {
303-
if (DEBUG) Log.d(TAG, "onLoading() called");
304-
305-
if (!isProgressLoopRunning()) startProgressLoop();
303+
super.onBlocked();
306304

307305
controlsVisibilityHandler.removeCallbacksAndMessages(null);
308306
animateView(controlsRoot, false, 300);
309307

310-
showAndAnimateControl(-1, true);
311-
playbackSeekBar.setEnabled(true);
312-
playbackSeekBar.setProgress(0);
313-
308+
playbackSeekBar.setEnabled(false);
314309
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
315310
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
316311
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
@@ -323,12 +318,19 @@ public void onBlocked() {
323318

324319
@Override
325320
public void onPlaying() {
326-
if (DEBUG) Log.d(TAG, "onPlaying() called");
327-
if (!isProgressLoopRunning()) startProgressLoop();
321+
super.onPlaying();
322+
328323
showAndAnimateControl(-1, true);
324+
325+
playbackSeekBar.setEnabled(true);
326+
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
327+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
328+
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
329+
329330
loadingPanel.setVisibility(View.GONE);
330331
showControlsThenHide();
331332
animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200);
333+
animateView(endScreen, false, 0);
332334
}
333335

334336
@Override
@@ -353,24 +355,13 @@ public void onPausedSeek() {
353355

354356
@Override
355357
public void onCompleted() {
356-
if (DEBUG) Log.d(TAG, "onCompleted() called");
357-
358-
if (isProgressLoopRunning()) stopProgressLoop();
358+
super.onCompleted();
359359

360360
showControls(500);
361361
animateView(endScreen, true, 800);
362362
animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200);
363363
loadingPanel.setVisibility(View.GONE);
364364

365-
playbackSeekBar.setMax((int) simpleExoPlayer.getDuration());
366-
playbackSeekBar.setProgress(playbackSeekBar.getMax());
367-
playbackSeekBar.setEnabled(false);
368-
playbackEndTime.setText(getTimeString(playbackSeekBar.getMax()));
369-
playbackCurrentTime.setText(playbackEndTime.getText());
370-
// Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again
371-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
372-
playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
373-
374365
animateView(surfaceForeground, true, 100);
375366
}
376367

app/src/main/java/org/schabi/newpipe/player/playback/PlaybackManager.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class PlaybackManager {
4545
private CompositeDisposable disposables;
4646

4747
private boolean isBlocked;
48+
private boolean hasReset;
4849

4950
public PlaybackManager(@NonNull final PlaybackListener listener,
5051
@NonNull final PlayQueue playQueue) {
@@ -72,8 +73,8 @@ public int getCurrentSourceIndex() {
7273
return sourceToQueueIndex.indexOf(playQueue.getIndex());
7374
}
7475

75-
public int size() {
76-
return sourceToQueueIndex.size();
76+
public int expectedTimelineSize() {
77+
return sources.getSize();
7778
}
7879

7980
public void dispose() {
@@ -178,6 +179,11 @@ private boolean tryBlock() {
178179

179180
private boolean tryUnblock() {
180181
if (isPlayQueueReady() && isCurrentIndexLoaded() && isBlocked) {
182+
if (hasReset) {
183+
playbackListener.prepare(sources);
184+
hasReset = false;
185+
}
186+
181187
isBlocked = false;
182188
playbackListener.unblock();
183189
return true;
@@ -249,7 +255,7 @@ private void resetSources() {
249255
if (this.sourceToQueueIndex != null) this.sourceToQueueIndex.clear();
250256

251257
this.sources = new DynamicConcatenatingMediaSource();
252-
playbackListener.prepare(this.sources);
258+
this.hasReset = true;
253259
}
254260

255261
/*//////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)