Extend ExoPlayer builders with useLazyPreparations and AnalyticsCollector.

Both values are needed to set-up the ExoPlayer instances for playlists.

PiperOrigin-RevId: 264146052
This commit is contained in:
tonihei 2019-08-19 14:48:42 +01:00 committed by Toni
parent 17d8e3728f
commit ebf8dc9973
2 changed files with 66 additions and 0 deletions

View file

@ -19,6 +19,7 @@ import android.content.Context;
import android.os.Looper; import android.os.Looper;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.analytics.AnalyticsCollector;
import com.google.android.exoplayer2.audio.MediaCodecAudioRenderer; import com.google.android.exoplayer2.audio.MediaCodecAudioRenderer;
import com.google.android.exoplayer2.metadata.MetadataRenderer; import com.google.android.exoplayer2.metadata.MetadataRenderer;
import com.google.android.exoplayer2.source.ClippingMediaSource; import com.google.android.exoplayer2.source.ClippingMediaSource;
@ -138,6 +139,8 @@ public interface ExoPlayer extends Player {
private LoadControl loadControl; private LoadControl loadControl;
private BandwidthMeter bandwidthMeter; private BandwidthMeter bandwidthMeter;
private Looper looper; private Looper looper;
private AnalyticsCollector analyticsCollector;
private boolean useLazyPreparation;
private boolean buildCalled; private boolean buildCalled;
/** /**
@ -152,6 +155,8 @@ public interface ExoPlayer extends Player {
* <li>{@link Looper}: The {@link Looper} associated with the current thread, or the {@link * <li>{@link Looper}: The {@link Looper} associated with the current thread, or the {@link
* Looper} of the application's main thread if the current thread doesn't have a {@link * Looper} of the application's main thread if the current thread doesn't have a {@link
* Looper} * Looper}
* <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT}
* <li>{@code useLazyPreparation}: {@code true}
* <li>{@link Clock}: {@link Clock#DEFAULT} * <li>{@link Clock}: {@link Clock#DEFAULT}
* </ul> * </ul>
* *
@ -165,6 +170,8 @@ public interface ExoPlayer extends Player {
new DefaultLoadControl(), new DefaultLoadControl(),
DefaultBandwidthMeter.getSingletonInstance(context), DefaultBandwidthMeter.getSingletonInstance(context),
Util.getLooper(), Util.getLooper(),
new AnalyticsCollector(Clock.DEFAULT),
/* useLazyPreparation= */ true,
Clock.DEFAULT); Clock.DEFAULT);
} }
@ -180,6 +187,8 @@ public interface ExoPlayer extends Player {
* @param loadControl A {@link LoadControl}. * @param loadControl A {@link LoadControl}.
* @param bandwidthMeter A {@link BandwidthMeter}. * @param bandwidthMeter A {@link BandwidthMeter}.
* @param looper A {@link Looper} that must be used for all calls to the player. * @param looper A {@link Looper} that must be used for all calls to the player.
* @param analyticsCollector An {@link AnalyticsCollector}.
* @param useLazyPreparation Whether media sources should be initialized lazily.
* @param clock A {@link Clock}. Should always be {@link Clock#DEFAULT}. * @param clock A {@link Clock}. Should always be {@link Clock#DEFAULT}.
*/ */
public Builder( public Builder(
@ -188,6 +197,8 @@ public interface ExoPlayer extends Player {
LoadControl loadControl, LoadControl loadControl,
BandwidthMeter bandwidthMeter, BandwidthMeter bandwidthMeter,
Looper looper, Looper looper,
AnalyticsCollector analyticsCollector,
boolean useLazyPreparation,
Clock clock) { Clock clock) {
Assertions.checkArgument(renderers.length > 0); Assertions.checkArgument(renderers.length > 0);
this.renderers = renderers; this.renderers = renderers;
@ -195,6 +206,8 @@ public interface ExoPlayer extends Player {
this.loadControl = loadControl; this.loadControl = loadControl;
this.bandwidthMeter = bandwidthMeter; this.bandwidthMeter = bandwidthMeter;
this.looper = looper; this.looper = looper;
this.analyticsCollector = analyticsCollector;
this.useLazyPreparation = useLazyPreparation;
this.clock = clock; this.clock = clock;
} }
@ -251,6 +264,36 @@ public interface ExoPlayer extends Player {
return this; return this;
} }
/**
* Sets the {@link AnalyticsCollector} that will collect and forward all player events.
*
* @param analyticsCollector An {@link AnalyticsCollector}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector) {
Assertions.checkState(!buildCalled);
this.analyticsCollector = analyticsCollector;
return this;
}
/**
* Sets whether media sources should be initialized lazily.
*
* <p>If false, all initial preparation steps (e.g., manifest loads) happen immediately. If
* true, these initial preparations are triggered only when the player starts buffering the
* media.
*
* @param useLazyPreparation Whether to use lazy preparation.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setUseLazyPreparation(boolean useLazyPreparation) {
Assertions.checkState(!buildCalled);
this.useLazyPreparation = useLazyPreparation;
return this;
}
/** /**
* Sets the {@link Clock} that will be used by the player. Should only be set for testing * Sets the {@link Clock} that will be used by the player. Should only be set for testing
* purposes. * purposes.

View file

@ -95,6 +95,7 @@ public class SimpleExoPlayer extends BasePlayer
private BandwidthMeter bandwidthMeter; private BandwidthMeter bandwidthMeter;
private AnalyticsCollector analyticsCollector; private AnalyticsCollector analyticsCollector;
private Looper looper; private Looper looper;
private boolean useLazyPreparation;
private boolean buildCalled; private boolean buildCalled;
/** /**
@ -115,6 +116,7 @@ public class SimpleExoPlayer extends BasePlayer
* Looper} of the application's main thread if the current thread doesn't have a {@link * Looper} of the application's main thread if the current thread doesn't have a {@link
* Looper} * Looper}
* <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT} * <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT}
* <li>{@code useLazyPreparation}: {@code true}
* <li>{@link Clock}: {@link Clock#DEFAULT} * <li>{@link Clock}: {@link Clock#DEFAULT}
* </ul> * </ul>
* *
@ -142,6 +144,7 @@ public class SimpleExoPlayer extends BasePlayer
DefaultBandwidthMeter.getSingletonInstance(context), DefaultBandwidthMeter.getSingletonInstance(context),
Util.getLooper(), Util.getLooper(),
new AnalyticsCollector(Clock.DEFAULT), new AnalyticsCollector(Clock.DEFAULT),
/* useLazyPreparation= */ true,
Clock.DEFAULT); Clock.DEFAULT);
} }
@ -160,6 +163,7 @@ public class SimpleExoPlayer extends BasePlayer
* @param bandwidthMeter A {@link BandwidthMeter}. * @param bandwidthMeter A {@link BandwidthMeter}.
* @param looper A {@link Looper} that must be used for all calls to the player. * @param looper A {@link Looper} that must be used for all calls to the player.
* @param analyticsCollector An {@link AnalyticsCollector}. * @param analyticsCollector An {@link AnalyticsCollector}.
* @param useLazyPreparation Whether media sources should be initialized lazily.
* @param clock A {@link Clock}. Should always be {@link Clock#DEFAULT}. * @param clock A {@link Clock}. Should always be {@link Clock#DEFAULT}.
*/ */
public Builder( public Builder(
@ -170,6 +174,7 @@ public class SimpleExoPlayer extends BasePlayer
BandwidthMeter bandwidthMeter, BandwidthMeter bandwidthMeter,
Looper looper, Looper looper,
AnalyticsCollector analyticsCollector, AnalyticsCollector analyticsCollector,
boolean useLazyPreparation,
Clock clock) { Clock clock) {
this.context = context; this.context = context;
this.renderersFactory = renderersFactory; this.renderersFactory = renderersFactory;
@ -178,6 +183,7 @@ public class SimpleExoPlayer extends BasePlayer
this.bandwidthMeter = bandwidthMeter; this.bandwidthMeter = bandwidthMeter;
this.looper = looper; this.looper = looper;
this.analyticsCollector = analyticsCollector; this.analyticsCollector = analyticsCollector;
this.useLazyPreparation = useLazyPreparation;
this.clock = clock; this.clock = clock;
} }
@ -247,6 +253,23 @@ public class SimpleExoPlayer extends BasePlayer
return this; return this;
} }
/**
* Sets whether media sources should be initialized lazily.
*
* <p>If false, all initial preparation steps (e.g., manifest loads) happen immediately. If
* true, these initial preparations are triggered only when the player starts buffering the
* media.
*
* @param useLazyPreparation Whether to use lazy preparation.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setUseLazyPreparation(boolean useLazyPreparation) {
Assertions.checkState(!buildCalled);
this.useLazyPreparation = useLazyPreparation;
return this;
}
/** /**
* Sets the {@link Clock} that will be used by the player. Should only be set for testing * Sets the {@link Clock} that will be used by the player. Should only be set for testing
* purposes. * purposes.