mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-30 23:03:00 +00:00 
			
		
		
		
	-Fixed activity pause and resume lifecycle.
This commit is contained in:
		 John Zhen M
					John Zhen M
				
			
				
					committed by
					
						 John Zhen Mo
						John Zhen Mo
					
				
			
			
				
	
			
			
			 John Zhen Mo
						John Zhen Mo
					
				
			
						parent
						
							174d040ca3
						
					
				
				
					commit
					f9eb2a1ee5
				
			| @@ -55,7 +55,7 @@ import org.schabi.newpipe.util.ThemeHelper; | |||||||
|  * |  * | ||||||
|  * @author mauriciocolli |  * @author mauriciocolli | ||||||
|  */ |  */ | ||||||
| public class BackgroundPlayer extends Service { | public final class BackgroundPlayer extends Service { | ||||||
|     private static final String TAG = "BackgroundPlayer"; |     private static final String TAG = "BackgroundPlayer"; | ||||||
|     private static final boolean DEBUG = BasePlayer.DEBUG; |     private static final boolean DEBUG = BasePlayer.DEBUG; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -171,7 +171,6 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|     public BasePlayer(Context context) { |     public BasePlayer(Context context) { | ||||||
|         this.context = context; |         this.context = context; | ||||||
|         this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |         this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | ||||||
|         this.audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); |  | ||||||
|  |  | ||||||
|         this.broadcastReceiver = new BroadcastReceiver() { |         this.broadcastReceiver = new BroadcastReceiver() { | ||||||
|             @Override |             @Override | ||||||
| @@ -329,6 +328,12 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|             audioManager.abandonAudioFocus(this); |             audioManager.abandonAudioFocus(this); | ||||||
|             audioManager = null; |             audioManager = null; | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void destroy() { | ||||||
|  |         if (DEBUG) Log.d(TAG, "destroy() called"); | ||||||
|  |         destroyPlayer(); | ||||||
|  |  | ||||||
|         if (playQueue != null) { |         if (playQueue != null) { | ||||||
|             playQueue.dispose(); |             playQueue.dispose(); | ||||||
|             playQueue = null; |             playQueue = null; | ||||||
| @@ -337,11 +342,7 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|             playbackManager.dispose(); |             playbackManager.dispose(); | ||||||
|             playbackManager = null; |             playbackManager = null; | ||||||
|         } |         } | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public void destroy() { |  | ||||||
|         if (DEBUG) Log.d(TAG, "destroy() called"); |  | ||||||
|         destroyPlayer(); |  | ||||||
|         unregisterBroadcastReceiver(); |         unregisterBroadcastReceiver(); | ||||||
|  |  | ||||||
|         simpleExoPlayer = null; |         simpleExoPlayer = null; | ||||||
| @@ -557,7 +558,7 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|         if (isCurrentWindowCorrect && getCurrentState() == STATE_PLAYING) return; |         if (isCurrentWindowCorrect && getCurrentState() == STATE_PLAYING) return; | ||||||
|  |  | ||||||
|         // Check timeline is up-to-date and has window |         // 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; |         if (simpleExoPlayer.getCurrentTimeline().getWindowCount() <= currentSourceIndex) return; | ||||||
|  |  | ||||||
|         // Check if window is ready |         // Check if window is ready | ||||||
| @@ -637,7 +638,8 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|                 changeState(playWhenReady ? STATE_PLAYING : STATE_PAUSED); |                 changeState(playWhenReady ? STATE_PLAYING : STATE_PAUSED); | ||||||
|                 break; |                 break; | ||||||
|             case Player.STATE_ENDED: // 4 |             case Player.STATE_ENDED: // 4 | ||||||
|                 if (isPrepared) { |                 // Ensure the current window is loaded | ||||||
|  |                 if (simpleExoPlayer.isCurrentWindowSeekable()) { | ||||||
|                     changeState(STATE_COMPLETED); |                     changeState(STATE_COMPLETED); | ||||||
|                     isPrepared = false; |                     isPrepared = false; | ||||||
|                 } |                 } | ||||||
| @@ -742,7 +744,10 @@ public abstract class BasePlayer implements Player.EventListener, | |||||||
|         if (!isPlaying()) audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); |         if (!isPlaying()) audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); | ||||||
|         else audioManager.abandonAudioFocus(this); |         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()); |         simpleExoPlayer.setPlayWhenReady(!isPlaying()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -20,7 +20,6 @@ | |||||||
| package org.schabi.newpipe.player; | package org.schabi.newpipe.player; | ||||||
|  |  | ||||||
| import android.app.Activity; | import android.app.Activity; | ||||||
| import android.content.Context; |  | ||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
| import android.content.pm.ActivityInfo; | import android.content.pm.ActivityInfo; | ||||||
| import android.graphics.Color; | import android.graphics.Color; | ||||||
| @@ -55,11 +54,10 @@ import static org.schabi.newpipe.util.AnimationUtils.animateView; | |||||||
|  * |  * | ||||||
|  * @author mauriciocolli |  * @author mauriciocolli | ||||||
|  */ |  */ | ||||||
| public class MainVideoPlayer extends Activity { | public final class MainVideoPlayer extends Activity { | ||||||
|     private static final String TAG = ".MainVideoPlayer"; |     private static final String TAG = ".MainVideoPlayer"; | ||||||
|     private static final boolean DEBUG = BasePlayer.DEBUG; |     private static final boolean DEBUG = BasePlayer.DEBUG; | ||||||
|  |  | ||||||
|     private AudioManager audioManager; |  | ||||||
|     private GestureDetector gestureDetector; |     private GestureDetector gestureDetector; | ||||||
|  |  | ||||||
|     private boolean activityPaused; |     private boolean activityPaused; | ||||||
| @@ -76,7 +74,6 @@ public class MainVideoPlayer extends Activity { | |||||||
|         ThemeHelper.setTheme(this); |         ThemeHelper.setTheme(this); | ||||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK); |         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK); | ||||||
|         setVolumeControlStream(AudioManager.STREAM_MUSIC); |         setVolumeControlStream(AudioManager.STREAM_MUSIC); | ||||||
|         audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |  | ||||||
|  |  | ||||||
|         if (getIntent() == null) { |         if (getIntent() == null) { | ||||||
|             Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show(); |             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"); |         if (DEBUG) Log.d(TAG, "onStop() called"); | ||||||
|         activityPaused = true; |         activityPaused = true; | ||||||
|         if (playerImpl.getPlayer() != null) { |         if (playerImpl.getPlayer() != null) { | ||||||
|  |             playerImpl.wasPlaying = playerImpl.getPlayer().getPlayWhenReady(); | ||||||
|             playerImpl.setRecovery( |             playerImpl.setRecovery( | ||||||
|                     playerImpl.getCurrentQueueIndex(), |                     playerImpl.getCurrentQueueIndex(), | ||||||
|                     (int) playerImpl.getPlayer().getCurrentPosition() |                     (int) playerImpl.getPlayer().getCurrentPosition() | ||||||
| @@ -126,8 +124,10 @@ public class MainVideoPlayer extends Activity { | |||||||
|         if (activityPaused) { |         if (activityPaused) { | ||||||
|             playerImpl.initPlayer(); |             playerImpl.initPlayer(); | ||||||
|             playerImpl.getPlayPauseButton().setImageResource(R.drawable.ic_play_arrow_white); |             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; |             activityPaused = false; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -495,7 +495,7 @@ public class MainVideoPlayer extends Activity { | |||||||
|         private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f; |         private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f; | ||||||
|         private float currentBrightness = .5f; |         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 float stepsVolume = 15, stepVolume = (float) Math.ceil(maxVolume / stepsVolume), minVolume = 0; | ||||||
|  |  | ||||||
|         private final String brightnessUnicode = new String(Character.toChars(0x2600)); |         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) { |             if (e1.getX() > playerImpl.getRootView().getWidth() / 2) { | ||||||
|                 double floor = Math.floor(up ? stepVolume : -stepVolume); |                 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 >= maxVolume) currentVolume = maxVolume; | ||||||
|                 if (currentVolume <= minVolume) currentVolume = (int) minVolume; |                 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); |                 if (DEBUG) Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume); | ||||||
|                 playerImpl.getVolumeTextView().setText(volumeUnicode + " " + Math.round((((float) currentVolume) / maxVolume) * 100) + "%"); |                 playerImpl.getVolumeTextView().setText(volumeUnicode + " " + Math.round((((float) currentVolume) / maxVolume) * 100) + "%"); | ||||||
|   | |||||||
| @@ -87,7 +87,7 @@ import static org.schabi.newpipe.util.AnimationUtils.animateView; | |||||||
|  * |  * | ||||||
|  * @author mauriciocolli |  * @author mauriciocolli | ||||||
|  */ |  */ | ||||||
| public class PopupVideoPlayer extends Service { | public final class PopupVideoPlayer extends Service { | ||||||
|     private static final String TAG = ".PopupVideoPlayer"; |     private static final String TAG = ".PopupVideoPlayer"; | ||||||
|     private static final boolean DEBUG = BasePlayer.DEBUG; |     private static final boolean DEBUG = BasePlayer.DEBUG; | ||||||
|     private static final int SHUTDOWN_FLING_VELOCITY = 10000; |     private static final int SHUTDOWN_FLING_VELOCITY = 10000; | ||||||
|   | |||||||
| @@ -300,17 +300,12 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer. | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onBlocked() { |     public void onBlocked() { | ||||||
|         if (DEBUG) Log.d(TAG, "onLoading() called"); |         super.onBlocked(); | ||||||
|  |  | ||||||
|         if (!isProgressLoopRunning()) startProgressLoop(); |  | ||||||
|  |  | ||||||
|         controlsVisibilityHandler.removeCallbacksAndMessages(null); |         controlsVisibilityHandler.removeCallbacksAndMessages(null); | ||||||
|         animateView(controlsRoot, false, 300); |         animateView(controlsRoot, false, 300); | ||||||
|  |  | ||||||
|         showAndAnimateControl(-1, true); |         playbackSeekBar.setEnabled(false); | ||||||
|         playbackSeekBar.setEnabled(true); |  | ||||||
|         playbackSeekBar.setProgress(0); |  | ||||||
|  |  | ||||||
|         // Bug on lower api, disabling and enabling the seekBar resets the thumb color -.-, so sets the color again |         // 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) |         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) | ||||||
|             playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); |             playbackSeekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); | ||||||
| @@ -323,12 +318,19 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer. | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onPlaying() { |     public void onPlaying() { | ||||||
|         if (DEBUG) Log.d(TAG, "onPlaying() called"); |         super.onPlaying(); | ||||||
|         if (!isProgressLoopRunning()) startProgressLoop(); |  | ||||||
|         showAndAnimateControl(-1, true); |         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); |         loadingPanel.setVisibility(View.GONE); | ||||||
|         showControlsThenHide(); |         showControlsThenHide(); | ||||||
|         animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200); |         animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200); | ||||||
|  |         animateView(endScreen, false, 0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -353,24 +355,13 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer. | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onCompleted() { |     public void onCompleted() { | ||||||
|         if (DEBUG) Log.d(TAG, "onCompleted() called"); |         super.onCompleted(); | ||||||
|  |  | ||||||
|         if (isProgressLoopRunning()) stopProgressLoop(); |  | ||||||
|  |  | ||||||
|         showControls(500); |         showControls(500); | ||||||
|         animateView(endScreen, true, 800); |         animateView(endScreen, true, 800); | ||||||
|         animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200); |         animateView(currentDisplaySeek, AnimationUtils.Type.SCALE_AND_ALPHA, false, 200); | ||||||
|         loadingPanel.setVisibility(View.GONE); |         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); |         animateView(surfaceForeground, true, 100); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -45,6 +45,7 @@ public class PlaybackManager { | |||||||
|     private CompositeDisposable disposables; |     private CompositeDisposable disposables; | ||||||
|  |  | ||||||
|     private boolean isBlocked; |     private boolean isBlocked; | ||||||
|  |     private boolean hasReset; | ||||||
|  |  | ||||||
|     public PlaybackManager(@NonNull final PlaybackListener listener, |     public PlaybackManager(@NonNull final PlaybackListener listener, | ||||||
|                            @NonNull final PlayQueue playQueue) { |                            @NonNull final PlayQueue playQueue) { | ||||||
| @@ -72,8 +73,8 @@ public class PlaybackManager { | |||||||
|         return sourceToQueueIndex.indexOf(playQueue.getIndex()); |         return sourceToQueueIndex.indexOf(playQueue.getIndex()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public int size() { |     public int expectedTimelineSize() { | ||||||
|         return sourceToQueueIndex.size(); |         return sources.getSize(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void dispose() { |     public void dispose() { | ||||||
| @@ -178,6 +179,11 @@ public class PlaybackManager { | |||||||
|  |  | ||||||
|     private boolean tryUnblock() { |     private boolean tryUnblock() { | ||||||
|         if (isPlayQueueReady() && isCurrentIndexLoaded() && isBlocked) { |         if (isPlayQueueReady() && isCurrentIndexLoaded() && isBlocked) { | ||||||
|  |             if (hasReset) { | ||||||
|  |                 playbackListener.prepare(sources); | ||||||
|  |                 hasReset = false; | ||||||
|  |             } | ||||||
|  |  | ||||||
|             isBlocked = false; |             isBlocked = false; | ||||||
|             playbackListener.unblock(); |             playbackListener.unblock(); | ||||||
|             return true; |             return true; | ||||||
| @@ -249,7 +255,7 @@ public class PlaybackManager { | |||||||
|         if (this.sourceToQueueIndex != null) this.sourceToQueueIndex.clear(); |         if (this.sourceToQueueIndex != null) this.sourceToQueueIndex.clear(); | ||||||
|  |  | ||||||
|         this.sources = new DynamicConcatenatingMediaSource(); |         this.sources = new DynamicConcatenatingMediaSource(); | ||||||
|         playbackListener.prepare(this.sources); |         this.hasReset = true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /*////////////////////////////////////////////////////////////////////////// |     /*////////////////////////////////////////////////////////////////////////// | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user