From 4d2a2384d685b09385ace1f2161088e5d9dfdba2 Mon Sep 17 00:00:00 2001 From: tonihei Date: Thu, 10 Sep 2020 12:12:23 +0100 Subject: [PATCH] Add convenience constructor methods. When passing in ExtractorFactory instances to SimpleExoPlayer.Builder or DefaultMediaSourceFactory, we currently need to pass in one other instance (RenderersFactory or DataSource.Factory), that developers will often set to its default. To avoid specifying these defaults, these new convience methods allow to just set the ExtractorsFactory if required. PiperOrigin-RevId: 330908002 --- .../android/exoplayer2/SimpleExoPlayer.java | 31 ++++++++++++------- .../source/DefaultMediaSourceFactory.java | 16 ++++++++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java index ba3a375a37..39490d5709 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java @@ -54,7 +54,6 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; -import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Clock; import com.google.android.exoplayer2.util.Log; @@ -118,11 +117,11 @@ public class SimpleExoPlayer extends BasePlayer /** * Creates a builder. * - *

Use {@link #Builder(Context, RenderersFactory)} or {@link #Builder(Context, - * RenderersFactory, ExtractorsFactory)} instead, if you intend to provide a custom {@link - * RenderersFactory} or a custom {@link ExtractorsFactory}. This is to ensure that ProGuard or - * R8 can remove ExoPlayer's {@link DefaultRenderersFactory} and {@link - * DefaultExtractorsFactory} from the APK. + *

Use {@link #Builder(Context, RenderersFactory)}, {@link #Builder(Context, + * RenderersFactory)} or {@link #Builder(Context, RenderersFactory, ExtractorsFactory)} instead, + * if you intend to provide a custom {@link RenderersFactory} or a custom {@link + * ExtractorsFactory}. This is to ensure that ProGuard or R8 can remove ExoPlayer's {@link + * DefaultRenderersFactory} and {@link DefaultExtractorsFactory} from the APK. * *

The builder uses the following default values: * @@ -167,6 +166,19 @@ public class SimpleExoPlayer extends BasePlayer this(context, renderersFactory, new DefaultExtractorsFactory()); } + /** + * Creates a builder with a custom {@link ExtractorsFactory}. + * + *

See {@link #Builder(Context)} for a list of default values. + * + * @param context A {@link Context}. + * @param extractorsFactory An {@link ExtractorsFactory} used to extract progressive media from + * its container. + */ + public Builder(Context context, ExtractorsFactory extractorsFactory) { + this(context, new DefaultRenderersFactory(context), extractorsFactory); + } + /** * Creates a builder with a custom {@link RenderersFactory} and {@link ExtractorsFactory}. * @@ -184,10 +196,7 @@ public class SimpleExoPlayer extends BasePlayer context, renderersFactory, new DefaultTrackSelector(context), - new DefaultMediaSourceFactory( - new DefaultDataSourceFactory( - context, Util.getUserAgent(context, ExoPlayerLibraryInfo.VERSION_SLASHY)), - extractorsFactory), + new DefaultMediaSourceFactory(context, extractorsFactory), new DefaultLoadControl(), DefaultBandwidthMeter.getSingletonInstance(context), new AnalyticsCollector(Clock.DEFAULT)); @@ -570,7 +579,7 @@ public class SimpleExoPlayer extends BasePlayer Clock clock, Looper applicationLooper) { this( - new Builder(context, renderersFactory, new DefaultExtractorsFactory()) + new Builder(context, renderersFactory) .setTrackSelector(trackSelector) .setMediaSourceFactory(mediaSourceFactory) .setLoadControl(loadControl) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java index f4fc7e4afe..3f1c03d3b1 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java @@ -65,9 +65,8 @@ import java.util.List; * MediaItem.PlaybackProperties#uri uri} doesn't match one of the above. It tries to infer the * required extractor by using the {@link * com.google.android.exoplayer2.extractor.DefaultExtractorsFactory} or the {@link - * ExtractorsFactory} provided in {@link #DefaultMediaSourceFactory(DataSource.Factory, - * ExtractorsFactory)}. An {@link UnrecognizedInputFormatException} is thrown if none of the - * available extractors can read the stream. + * ExtractorsFactory} provided in the constructor. An {@link UnrecognizedInputFormatException} + * is thrown if none of the available extractors can read the stream. * * *

Ad support for media items with ad tag URIs

@@ -117,6 +116,17 @@ public final class DefaultMediaSourceFactory implements MediaSourceFactory { this(new DefaultDataSourceFactory(context)); } + /** + * Creates a new instance. + * + * @param context Any context. + * @param extractorsFactory An {@link ExtractorsFactory} used to extract progressive media from + * its container. + */ + public DefaultMediaSourceFactory(Context context, ExtractorsFactory extractorsFactory) { + this(new DefaultDataSourceFactory(context), extractorsFactory); + } + /** * Creates a new instance. *