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 androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.analytics.AnalyticsCollector;
import com.google.android.exoplayer2.audio.MediaCodecAudioRenderer;
import com.google.android.exoplayer2.metadata.MetadataRenderer;
import com.google.android.exoplayer2.source.ClippingMediaSource;
@ -138,6 +139,8 @@ public interface ExoPlayer extends Player {
private LoadControl loadControl;
private BandwidthMeter bandwidthMeter;
private Looper looper;
private AnalyticsCollector analyticsCollector;
private boolean useLazyPreparation;
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
* Looper} of the application's main thread if the current thread doesn't have a {@link
* Looper}
* <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT}
* <li>{@code useLazyPreparation}: {@code true}
* <li>{@link Clock}: {@link Clock#DEFAULT}
* </ul>
*
@ -165,6 +170,8 @@ public interface ExoPlayer extends Player {
new DefaultLoadControl(),
DefaultBandwidthMeter.getSingletonInstance(context),
Util.getLooper(),
new AnalyticsCollector(Clock.DEFAULT),
/* useLazyPreparation= */ true,
Clock.DEFAULT);
}
@ -180,6 +187,8 @@ public interface ExoPlayer extends Player {
* @param loadControl A {@link LoadControl}.
* @param bandwidthMeter A {@link BandwidthMeter}.
* @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}.
*/
public Builder(
@ -188,6 +197,8 @@ public interface ExoPlayer extends Player {
LoadControl loadControl,
BandwidthMeter bandwidthMeter,
Looper looper,
AnalyticsCollector analyticsCollector,
boolean useLazyPreparation,
Clock clock) {
Assertions.checkArgument(renderers.length > 0);
this.renderers = renderers;
@ -195,6 +206,8 @@ public interface ExoPlayer extends Player {
this.loadControl = loadControl;
this.bandwidthMeter = bandwidthMeter;
this.looper = looper;
this.analyticsCollector = analyticsCollector;
this.useLazyPreparation = useLazyPreparation;
this.clock = clock;
}
@ -251,6 +264,36 @@ public interface ExoPlayer extends Player {
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
* purposes.

View file

@ -95,6 +95,7 @@ public class SimpleExoPlayer extends BasePlayer
private BandwidthMeter bandwidthMeter;
private AnalyticsCollector analyticsCollector;
private Looper looper;
private boolean useLazyPreparation;
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}
* <li>{@link AnalyticsCollector}: {@link AnalyticsCollector} with {@link Clock#DEFAULT}
* <li>{@code useLazyPreparation}: {@code true}
* <li>{@link Clock}: {@link Clock#DEFAULT}
* </ul>
*
@ -142,6 +144,7 @@ public class SimpleExoPlayer extends BasePlayer
DefaultBandwidthMeter.getSingletonInstance(context),
Util.getLooper(),
new AnalyticsCollector(Clock.DEFAULT),
/* useLazyPreparation= */ true,
Clock.DEFAULT);
}
@ -160,6 +163,7 @@ public class SimpleExoPlayer extends BasePlayer
* @param bandwidthMeter A {@link BandwidthMeter}.
* @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}.
*/
public Builder(
@ -170,6 +174,7 @@ public class SimpleExoPlayer extends BasePlayer
BandwidthMeter bandwidthMeter,
Looper looper,
AnalyticsCollector analyticsCollector,
boolean useLazyPreparation,
Clock clock) {
this.context = context;
this.renderersFactory = renderersFactory;
@ -178,6 +183,7 @@ public class SimpleExoPlayer extends BasePlayer
this.bandwidthMeter = bandwidthMeter;
this.looper = looper;
this.analyticsCollector = analyticsCollector;
this.useLazyPreparation = useLazyPreparation;
this.clock = clock;
}
@ -247,6 +253,23 @@ public class SimpleExoPlayer extends BasePlayer
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
* purposes.