Skip to content

Commit 45599c7

Browse files
committed
Completely close player when changing stream w/o autoplay
1 parent ff506ce commit 45599c7

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public final class VideoDetailFragment
201201
@Nullable
202202
private MainPlayer playerService;
203203
private Player player;
204-
private PlayerHolder playerHolder = PlayerHolder.getInstance();
204+
private final PlayerHolder playerHolder = PlayerHolder.getInstance();
205205

206206
/*//////////////////////////////////////////////////////////////////////////
207207
// Service management
@@ -762,7 +762,7 @@ public boolean onBackPressed() {
762762

763763
private void setupFromHistoryItem(final StackItem item) {
764764
setAutoPlay(false);
765-
hideMainPlayer();
765+
hideMainPlayerOnLoadingNewStream();
766766

767767
setInitialData(item.getServiceId(), item.getUrl(),
768768
item.getTitle() == null ? "" : item.getTitle(), item.getPlayQueue());
@@ -882,7 +882,7 @@ private void runWorker(final boolean forceLoad, final boolean addToBackStack) {
882882
.observeOn(AndroidSchedulers.mainThread())
883883
.subscribe(result -> {
884884
isLoading.set(false);
885-
hideMainPlayer();
885+
hideMainPlayerOnLoadingNewStream();
886886
if (result.getAgeLimit() != NO_AGE_LIMIT && !prefs.getBoolean(
887887
getString(R.string.show_age_restricted_content), false)) {
888888
hideAgeRestrictedContent();
@@ -1174,16 +1174,27 @@ private void openMainPlayer() {
11741174
ContextCompat.startForegroundService(activity, playerIntent);
11751175
}
11761176

1177-
private void hideMainPlayer() {
1177+
/**
1178+
* When the video detail fragment is already showing details for a video and the user opens a
1179+
* new one, the video detail fragment changes all of its old data to the new stream, so if there
1180+
* is a video player currently open it should be hidden. This method does exactly that. If
1181+
* autoplay is enabled, the underlying player is not stopped completely, since it is going to
1182+
* be reused in a few milliseconds and the flickering would be annoying.
1183+
*/
1184+
private void hideMainPlayerOnLoadingNewStream() {
11781185
if (!isPlayerServiceAvailable()
11791186
|| playerService.getView() == null
11801187
|| !player.videoPlayerSelected()) {
11811188
return;
11821189
}
11831190

11841191
removeVideoPlayerView();
1185-
playerService.stop(isAutoplayEnabled());
1186-
playerService.getView().setVisibility(View.GONE);
1192+
if (isAutoplayEnabled()) {
1193+
playerService.stopForImmediateReusing();
1194+
playerService.getView().setVisibility(View.GONE);
1195+
} else {
1196+
playerHolder.stopService();
1197+
}
11871198
}
11881199

11891200
private PlayQueue setupPlayQueueForIntent(final boolean append) {
@@ -1832,7 +1843,7 @@ public void onPlayerError(final ExoPlaybackException error) {
18321843
|| error.type == ExoPlaybackException.TYPE_UNEXPECTED) {
18331844
// Properly exit from fullscreen
18341845
toggleFullscreenIfInFullscreenMode();
1835-
hideMainPlayer();
1846+
hideMainPlayerOnLoadingNewStream();
18361847
}
18371848
}
18381849

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -133,32 +133,29 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
133133
return START_NOT_STICKY;
134134
}
135135

136-
public void stop(final boolean autoplayEnabled) {
136+
public void stopForImmediateReusing() {
137137
if (DEBUG) {
138-
Log.d(TAG, "stop() called");
138+
Log.d(TAG, "stopForImmediateReusing() called");
139139
}
140140

141141
if (!player.exoPlayerIsNull()) {
142142
player.saveWasPlaying();
143+
143144
// Releases wifi & cpu, disables keepScreenOn, etc.
144-
if (!autoplayEnabled) {
145-
player.pause();
146-
}
147145
// We can't just pause the player here because it will make transition
148146
// from one stream to a new stream not smooth
149147
player.smoothStopPlayer();
150148
player.setRecovery();
149+
151150
// Android TV will handle back button in case controls will be visible
152151
// (one more additional unneeded click while the player is hidden)
153152
player.hideControls(0, 0);
154153
player.closeItemsList();
154+
155155
// Notification shows information about old stream but if a user selects
156156
// a stream from backStack it's not actual anymore
157157
// So we should hide the notification at all.
158158
// When autoplay enabled such notification flashing is annoying so skip this case
159-
if (!autoplayEnabled) {
160-
NotificationUtil.getInstance().cancelNotificationAndStopForeground(this);
161-
}
162159
}
163160
}
164161

0 commit comments

Comments
 (0)