From 4f2b596062090e12b6f68486593acd0f5a950e59 Mon Sep 17 00:00:00 2001 From: tonihei Date: Wed, 20 Jun 2018 04:08:31 -0700 Subject: [PATCH] Allow to specify player looper at the time of ExoPlayer creation. Currently, the looper of the thread the player is created on is used (or the main looper if this thread doesn't have a looper). To allow more control over the threading, this change lets users specificy the looper which must be used to call player methods and which is used for event callbacks. Issue:#4278 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=201331564 --- RELEASENOTES.md | 2 + .../mediasession/MediaSessionConnector.java | 22 ++--- .../google/android/exoplayer2/ExoPlayer.java | 23 ++--- .../android/exoplayer2/ExoPlayerFactory.java | 83 ++++++++++++++++++- .../android/exoplayer2/ExoPlayerImpl.java | 27 ++++-- .../android/exoplayer2/SimpleExoPlayer.java | 54 +++++++++--- .../source/ConcatenatingMediaSource.java | 51 ++++-------- .../google/android/exoplayer2/util/Util.java | 14 +++- .../testutil/ExoPlayerTestRunner.java | 4 +- .../testutil/MediaSourceTestRunner.java | 5 ++ .../exoplayer2/testutil/StubExoPlayer.java | 5 ++ 11 files changed, 210 insertions(+), 80 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 3ae194c223c..87adaa88401 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -33,6 +33,8 @@ ([#4385](https://github.com/google/ExoPlayer/issues/4385)). * Expose all internal ID3 data stored in MP4 udta boxes, and switch from using CommentFrame to InternalFrame for frames with gapless metadata in MP4. +* Allow setting the `Looper`, which is used to access the player, in + `ExoPlayerFactory` ([#4278](https://github.com/google/ExoPlayer/issues/4278)). ### 2.8.2 ### diff --git a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java index 4bafaa43260..e8758cd05b1 100644 --- a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java +++ b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java @@ -19,7 +19,6 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; -import android.os.Looper; import android.os.ResultReceiver; import android.os.SystemClock; import android.support.annotation.NonNull; @@ -39,6 +38,7 @@ import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.util.ErrorMessageProvider; import com.google.android.exoplayer2.util.RepeatModeUtil; +import com.google.android.exoplayer2.util.Util; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -323,7 +323,6 @@ public interface CustomActionProvider { public final MediaSessionCompat mediaSession; private final MediaControllerCompat mediaController; - private final Handler handler; private final boolean doMaintainMetadata; private final ExoPlayerEventListener exoPlayerEventListener; private final MediaSessionCallback mediaSessionCallback; @@ -341,10 +340,9 @@ public interface CustomActionProvider { private RatingCallback ratingCallback; /** - * Creates an instance. Must be called on the same thread that is used to construct the player - * instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}. - *

- * Equivalent to {@code MediaSessionConnector(mediaSession, new DefaultPlaybackController())}. + * Creates an instance. + * + *

Equivalent to {@code MediaSessionConnector(mediaSession, new DefaultPlaybackController())}. * * @param mediaSession The {@link MediaSessionCompat} to connect to. */ @@ -353,8 +351,7 @@ public MediaSessionConnector(MediaSessionCompat mediaSession) { } /** - * Creates an instance. Must be called on the same thread that is used to construct the player - * instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}. + * Creates an instance. * *

Equivalent to {@code MediaSessionConnector(mediaSession, playbackController, true, null)}. * @@ -367,8 +364,7 @@ public MediaSessionConnector( } /** - * Creates an instance. Must be called on the same thread that is used to construct the player - * instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}. + * Creates an instance. * * @param mediaSession The {@link MediaSessionCompat} to connect to. * @param playbackController A {@link PlaybackController} for handling playback actions, or {@code @@ -388,8 +384,6 @@ public MediaSessionConnector( this.playbackController = playbackController != null ? playbackController : new DefaultPlaybackController(); this.metadataExtrasPrefix = metadataExtrasPrefix != null ? metadataExtrasPrefix : ""; - this.handler = new Handler(Looper.myLooper() != null ? Looper.myLooper() - : Looper.getMainLooper()); this.doMaintainMetadata = doMaintainMetadata; mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS); mediaController = mediaSession.getController(); @@ -401,7 +395,8 @@ public MediaSessionConnector( } /** - * Sets the player to be connected to the media session. + * Sets the player to be connected to the media session. Must be called on the same thread that is + * used to access the player. * *

The order in which any {@link CustomActionProvider}s are passed determines the order of the * actions published with the playback state of the session. @@ -428,6 +423,7 @@ public void setPlayer( this.customActionProviders = (player != null && customActionProviders != null) ? customActionProviders : new CustomActionProvider[0]; if (player != null) { + Handler handler = new Handler(Util.getLooper()); mediaSession.setCallback(mediaSessionCallback, handler); player.addListener(exoPlayerEventListener); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java index b97790d5fb2..ce43772d8db 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java @@ -89,12 +89,13 @@ * model"> * *