From dcd5197a19e4451fbe69b8b57e96e569dc31b194 Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 7 Oct 2019 07:54:31 +0200 Subject: [PATCH] Update to ExoPlayer 2.10.5 --- app/build.gradle | 2 +- .../player/helper/MediaSessionManager.java | 13 +-- .../mediasession/PlayQueueNavigator.java | 21 +++-- .../PlayQueuePlaybackController.java | 18 ++--- .../player/playback/CustomTrackSelector.java | 79 +++++++------------ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 7 files changed, 57 insertions(+), 82 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f1480066d..26344a9da 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,7 @@ android { ext { androidxLibVersion = '1.0.0' - exoPlayerLibVersion = '2.9.6' //2.10.5 + exoPlayerLibVersion = '2.10.5' roomDbLibVersion = '2.1.0' leakCanaryLibVersion = '1.5.4' //1.6.1 okHttpLibVersion = '3.12.1' diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java index 37d4c1df9..a5c703837 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java @@ -2,11 +2,12 @@ package org.schabi.newpipe.player.helper; import android.content.Context; import android.content.Intent; +import android.support.v4.media.session.MediaSessionCompat; +import android.view.KeyEvent; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.media.session.MediaButtonReceiver; -import android.support.v4.media.session.MediaSessionCompat; -import android.view.KeyEvent; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; @@ -27,10 +28,10 @@ public class MediaSessionManager { this.mediaSession = new MediaSessionCompat(context, TAG); this.mediaSession.setActive(true); - this.sessionConnector = new MediaSessionConnector(mediaSession, - new PlayQueuePlaybackController(callback)); + this.sessionConnector = new MediaSessionConnector(mediaSession); + this.sessionConnector.setControlDispatcher(new PlayQueuePlaybackController(callback)); this.sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); - this.sessionConnector.setPlayer(player, null); + this.sessionConnector.setPlayer(player); } @Nullable @@ -43,7 +44,7 @@ public class MediaSessionManager { * Should be called on player destruction to prevent leakage. * */ public void dispose() { - this.sessionConnector.setPlayer(null, null); + this.sessionConnector.setPlayer(null); this.sessionConnector.setQueueNavigator(null); this.mediaSession.setActive(false); this.mediaSession.release(); diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java index 72af2497f..ab0de08be 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java @@ -2,10 +2,12 @@ package org.schabi.newpipe.player.mediasession; import android.os.Bundle; import android.os.ResultReceiver; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import android.support.v4.media.session.MediaSessionCompat; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import com.google.android.exoplayer2.util.Util; @@ -63,17 +65,17 @@ public class PlayQueueNavigator implements MediaSessionConnector.QueueNavigator } @Override - public void onSkipToPrevious(Player player) { + public void onSkipToPrevious(Player player, ControlDispatcher controlDispatcher) { callback.onSkipToPrevious(); } @Override - public void onSkipToQueueItem(Player player, long id) { + public void onSkipToQueueItem(Player player, ControlDispatcher controlDispatcher, long id) { callback.onSkipToIndex((int) id); } @Override - public void onSkipToNext(Player player) { + public void onSkipToNext(Player player, ControlDispatcher controlDispatcher) { callback.onSkipToNext(); } @@ -100,12 +102,7 @@ public class PlayQueueNavigator implements MediaSessionConnector.QueueNavigator } @Override - public String[] getCommands() { - return new String[0]; - } - - @Override - public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) { - + public boolean onCommand(Player player, ControlDispatcher controlDispatcher, String command, Bundle extras, ResultReceiver cb) { + return false; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java index a460a1653..c377901f3 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.player.mediasession; +import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.Player; -import com.google.android.exoplayer2.ext.mediasession.DefaultPlaybackController; -public class PlayQueuePlaybackController extends DefaultPlaybackController { +public class PlayQueuePlaybackController extends DefaultControlDispatcher { private final MediaSessionCallback callback; public PlayQueuePlaybackController(final MediaSessionCallback callback) { @@ -12,12 +12,12 @@ public class PlayQueuePlaybackController extends DefaultPlaybackController { } @Override - public void onPlay(Player player) { - callback.onPlay(); - } - - @Override - public void onPause(Player player) { - callback.onPause(); + public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) { + if(playWhenReady) { + callback.onPlay(); + } else { + callback.onPause(); + } + return true; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java b/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java index fde826a96..bc9290684 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java @@ -1,15 +1,17 @@ package org.schabi.newpipe.player.playback; -import androidx.annotation.NonNull; import android.text.TextUtils; import android.util.Pair; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; -import com.google.android.exoplayer2.trackselection.FixedTrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.util.Assertions; @@ -21,7 +23,6 @@ import com.google.android.exoplayer2.util.Assertions; * a broader set of languages. * */ public class CustomTrackSelector extends DefaultTrackSelector { - private static final int WITHIN_RENDERER_CAPABILITIES_BONUS = 1000; private String preferredTextLanguage; @@ -41,23 +42,22 @@ public class CustomTrackSelector extends DefaultTrackSelector { } } - /** @see DefaultTrackSelector#formatHasLanguage(Format, String)*/ - protected static boolean formatHasLanguage(Format format, String language) { + private static boolean formatHasLanguage(Format format, String language) { return language != null && TextUtils.equals(language, format.language); } - /** @see DefaultTrackSelector#formatHasNoLanguage(Format)*/ - protected static boolean formatHasNoLanguage(Format format) { - return TextUtils.isEmpty(format.language) || formatHasLanguage(format, C.LANGUAGE_UNDETERMINED); - } - - /** @see DefaultTrackSelector#selectTextTrack(TrackGroupArray, int[][], Parameters) */ @Override - protected Pair selectTextTrack(TrackGroupArray groups, int[][] formatSupport, - Parameters params) { + @Nullable + protected Pair selectTextTrack( + TrackGroupArray groups, + int[][] formatSupport, + Parameters params, + @Nullable String selectedAudioLanguage) + throws ExoPlaybackException { TrackGroup selectedGroup = null; - int selectedTrackIndex = 0; - int selectedTrackScore = 0; + int selectedTrackIndex = C.INDEX_UNSET; + int newPipeTrackScore = 0; + TextTrackScore selectedTrackScore = null; for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) { TrackGroup trackGroup = groups.get(groupIndex); int[] trackFormatSupport = formatSupport[groupIndex]; @@ -65,41 +65,17 @@ public class CustomTrackSelector extends DefaultTrackSelector { if (isSupported(trackFormatSupport[trackIndex], params.exceedRendererCapabilitiesIfNecessary)) { Format format = trackGroup.getFormat(trackIndex); - int maskedSelectionFlags = - format.selectionFlags & ~params.disabledTextTrackSelectionFlags; - boolean isDefault = (maskedSelectionFlags & C.SELECTION_FLAG_DEFAULT) != 0; - boolean isForced = (maskedSelectionFlags & C.SELECTION_FLAG_FORCED) != 0; - int trackScore; - boolean preferredLanguageFound = formatHasLanguage(format, preferredTextLanguage); - if (preferredLanguageFound - || (params.selectUndeterminedTextLanguage && formatHasNoLanguage(format))) { - if (isDefault) { - trackScore = 8; - } else if (!isForced) { - // Prefer non-forced to forced if a preferred text language has been specified. Where - // both are provided the non-forced track will usually contain the forced subtitles as - // a subset. - trackScore = 6; - } else { - trackScore = 4; - } - trackScore += preferredLanguageFound ? 1 : 0; - } else if (isDefault) { - trackScore = 3; - } else if (isForced) { - if (formatHasLanguage(format, params.preferredAudioLanguage)) { - trackScore = 2; - } else { - trackScore = 1; - } - } else { - // Track should not be selected. - continue; - } - if (isSupported(trackFormatSupport[trackIndex], false)) { - trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS; - } - if (trackScore > selectedTrackScore) { + TextTrackScore trackScore = + new TextTrackScore( + format, params, trackFormatSupport[trackIndex], selectedAudioLanguage); + if(formatHasLanguage(format, preferredTextLanguage)) { + selectedGroup = trackGroup; + selectedTrackIndex = trackIndex; + selectedTrackScore = trackScore; + // found user selected match (perfect!) + break; + } else if (trackScore.isWithinConstraints + && (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0)) { selectedGroup = trackGroup; selectedTrackIndex = trackIndex; selectedTrackScore = trackScore; @@ -110,6 +86,7 @@ public class CustomTrackSelector extends DefaultTrackSelector { return selectedGroup == null ? null : Pair.create( - new FixedTrackSelection(selectedGroup, selectedTrackIndex), selectedTrackScore); + new TrackSelection.Definition(selectedGroup, selectedTrackIndex), + Assertions.checkNotNull(selectedTrackScore)); } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index a95f6dcc0..60a85021a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.android.tools.build:gradle:3.4.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e2a99f5b5..e98d29bd8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Sep 22 10:40:45 CEST 2019 +#Mon Oct 07 06:29:33 CEST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip