Skip to content

Commit ec6612d

Browse files
committed
Call exoPlayer.prepare() on PlaybackPreparer.onPrepare()
If a playbackPreparer is set, then instead of calling `player.prepare()`, the MediaSessionConnector will call `playbackPreparer.onPrepare(true)` instead, as seen below. This commit makes it so that playbackPreparer.onPrepare(true) restores the original behavior of just calling player.prepare(). From MediaSessionConnector -> MediaSessionCompat.Callback implementation: ```java @OverRide public void onPlay() { if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_PLAY)) { if (player.getPlaybackState() == Player.STATE_IDLE) { if (playbackPreparer != null) { playbackPreparer.onPrepare(/* playWhenReady= */ true); } else { player.prepare(); } } else if (player.getPlaybackState() == Player.STATE_ENDED) { seekTo(player, player.getCurrentMediaItemIndex(), C.TIME_UNSET); } Assertions.checkNotNull(player).play(); } } ```
1 parent 064e1d3 commit ec6612d

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,19 @@ public void onRenderedFirstFrame() {
13861386
public void onCues(@NonNull final CueGroup cueGroup) {
13871387
UIs.call(playerUi -> playerUi.onCues(cueGroup.cues));
13881388
}
1389+
1390+
/**
1391+
* To be called when the {@code PlaybackPreparer} set in the {@link MediaSessionConnector}
1392+
* receives an {@code onPrepare()} call. This function allows restoring the default behavior
1393+
* that would happen if there was no playback preparer set, i.e. to just call
1394+
* {@code player.prepare()}. You can find the default behavior in `onPlay()` inside the
1395+
* {@link MediaSessionConnector} file.
1396+
*/
1397+
public void onPrepare() {
1398+
if (!exoPlayerIsNull()) {
1399+
simpleExoPlayer.prepare();
1400+
}
1401+
}
13891402
//endregion
13901403

13911404

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ public void onCreate() {
9696
mediaBrowserPlaybackPreparer = new MediaBrowserPlaybackPreparer(
9797
this,
9898
sessionConnector::setCustomErrorMessage,
99-
() -> sessionConnector.setCustomErrorMessage(null)
99+
() -> sessionConnector.setCustomErrorMessage(null),
100+
(playWhenReady) -> {
101+
if (player != null) {
102+
player.onPrepare();
103+
}
104+
}
100105
);
101106
sessionConnector.setPlaybackPreparer(mediaBrowserPlaybackPreparer);
102107

app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserPlaybackPreparer.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.schabi.newpipe.util.ChannelTabHelper
2929
import org.schabi.newpipe.util.ExtractorHelper
3030
import org.schabi.newpipe.util.NavigationHelper
3131
import java.util.function.BiConsumer
32+
import java.util.function.Consumer
3233

3334
/**
3435
* This class is used to cleanly separate the Service implementation (in
@@ -40,11 +41,15 @@ import java.util.function.BiConsumer
4041
* @param setMediaSessionError takes an error String and an error code from [PlaybackStateCompat],
4142
* calls `sessionConnector.setCustomErrorMessage(errorString, errorCode)`
4243
* @param clearMediaSessionError calls `sessionConnector.setCustomErrorMessage(null)`
44+
* @param onPrepare takes playWhenReady, calls `player.prepare()`; this is needed because
45+
* `MediaSessionConnector`'s `onPlay()` method calls this class' [onPrepare] instead of
46+
* `player.prepare()` if the playback preparer is not null, but we want the original behavior
4347
*/
4448
class MediaBrowserPlaybackPreparer(
4549
private val context: Context,
4650
private val setMediaSessionError: BiConsumer<String, Int>, // error string, error code
4751
private val clearMediaSessionError: Runnable,
52+
private val onPrepare: Consumer<Boolean>,
4853
) : PlaybackPreparer {
4954
private val database = NewPipeDatabase.getInstance(context)
5055
private var disposable: Disposable? = null
@@ -59,7 +64,7 @@ class MediaBrowserPlaybackPreparer(
5964
}
6065

6166
override fun onPrepare(playWhenReady: Boolean) {
62-
// TODO handle onPrepare
67+
onPrepare.accept(playWhenReady)
6368
}
6469

6570
override fun onPrepareFromMediaId(mediaId: String, playWhenReady: Boolean, extras: Bundle?) {

0 commit comments

Comments
 (0)