diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java index f891bb9662..ef58571f08 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java @@ -1187,6 +1187,75 @@ public final class MediaItem implements Bundleable { /** Properties for a text track. */ public static final class Subtitle { + /** Builder for {@link Subtitle} instances. */ + public static final class Builder { + private Uri uri; + @Nullable private String mimeType; + @Nullable private String language; + @C.SelectionFlags private int selectionFlags; + @C.RoleFlags private int roleFlags; + @Nullable private String label; + + /** + * Constructs an instance. + * + * @param uri The {@link Uri} to the subtitle file. + */ + public Builder(Uri uri) { + this.uri = uri; + } + + private Builder(Subtitle subtitle) { + this.uri = subtitle.uri; + this.mimeType = subtitle.mimeType; + this.language = subtitle.language; + this.selectionFlags = subtitle.selectionFlags; + this.roleFlags = subtitle.roleFlags; + this.label = subtitle.label; + } + + /** Sets the {@link Uri} to the subtitle file. */ + public Builder setUri(Uri uri) { + this.uri = uri; + return this; + } + + /** Sets the MIME type. */ + public Builder setMimeType(String mimeType) { + this.mimeType = mimeType; + return this; + } + + /** Sets the optional language of the subtitle file. */ + public Builder setLanguage(@Nullable String language) { + this.language = language; + return this; + } + + /** Sets the flags used for track selection. */ + public Builder setSelectionFlags(@C.SelectionFlags int selectionFlags) { + this.selectionFlags = selectionFlags; + return this; + } + + /** Sets the role flags. These are used for track selection. */ + public Builder setRoleFlags(@C.RoleFlags int roleFlags) { + this.roleFlags = roleFlags; + return this; + } + + /** Sets the optional label for this subtitle track. */ + public Builder setLabel(@Nullable String label) { + this.label = label; + return this; + } + + /** Creates a {@link Subtitle} from the values of this builder. */ + public Subtitle build() { + return new Subtitle(this); + } + } + /** The {@link Uri} to the subtitle file. */ public final Uri uri; /** The optional MIME type of the subtitle file, or {@code null} if unspecified. */ @@ -1200,40 +1269,21 @@ public final class MediaItem implements Bundleable { /** The label. */ @Nullable public final String label; - /** - * Creates an instance. - * - * @param uri The {@link Uri URI} to the subtitle file. - * @param mimeType The MIME type. - * @param language The optional language. - */ + /** @deprecated Use {@link Builder} instead. */ + @Deprecated public Subtitle(Uri uri, String mimeType, @Nullable String language) { this(uri, mimeType, language, /* selectionFlags= */ 0); } - /** - * Creates an instance. - * - * @param uri The {@link Uri URI} to the subtitle file. - * @param mimeType The MIME type. - * @param language The optional language. - * @param selectionFlags The selection flags. - */ + /** @deprecated Use {@link Builder} instead. */ + @Deprecated public Subtitle( Uri uri, String mimeType, @Nullable String language, @C.SelectionFlags int selectionFlags) { this(uri, mimeType, language, selectionFlags, /* roleFlags= */ 0, /* label= */ null); } - /** - * Creates an instance. - * - * @param uri The {@link Uri URI} to the subtitle file. - * @param mimeType The MIME type. - * @param language The optional language. - * @param selectionFlags The selection flags. - * @param roleFlags The role flags. - * @param label The optional label. - */ + /** @deprecated Use {@link Builder} instead. */ + @Deprecated public Subtitle( Uri uri, String mimeType, @@ -1249,6 +1299,20 @@ public final class MediaItem implements Bundleable { this.label = label; } + private Subtitle(Builder builder) { + this.uri = builder.uri; + this.mimeType = builder.mimeType; + this.language = builder.language; + this.selectionFlags = builder.selectionFlags; + this.roleFlags = builder.roleFlags; + this.label = builder.label; + } + + /** Returns a {@link Builder} initialized with the values of this instance. */ + public Builder buildUpon() { + return new Builder(this); + } + @Override public boolean equals(@Nullable Object obj) { if (this == obj) { diff --git a/library/common/src/test/java/com/google/android/exoplayer2/MediaItemTest.java b/library/common/src/test/java/com/google/android/exoplayer2/MediaItemTest.java index d00a49ab67..e503dfa4e4 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/MediaItemTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/MediaItemTest.java @@ -258,9 +258,17 @@ public class MediaItemTest { } @Test + @SuppressWarnings("deprecation") // Using deprecated constructors public void builderSetSubtitles_setsSubtitles() { List subtitles = - Arrays.asList( + ImmutableList.of( + new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/es")) + .setMimeType(MimeTypes.TEXT_SSA) + .setLanguage(/* language= */ "es") + .setSelectionFlags(C.SELECTION_FLAG_FORCED) + .setRoleFlags(C.ROLE_FLAG_ALTERNATE) + .setLabel("label") + .build(), new MediaItem.Subtitle( Uri.parse(URI_STRING + "/en"), MimeTypes.APPLICATION_TTML, /* language= */ "en"), new MediaItem.Subtitle( @@ -531,14 +539,14 @@ public class MediaItemTest { .setLiveMinOffsetMs(2222) .setLiveMaxOffsetMs(4444) .setSubtitles( - Collections.singletonList( - new MediaItem.Subtitle( - Uri.parse(URI_STRING + "/en"), - MimeTypes.APPLICATION_TTML, - /* language= */ "en", - C.SELECTION_FLAG_FORCED, - C.ROLE_FLAG_ALTERNATE, - "label"))) + ImmutableList.of( + new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/en")) + .setMimeType(MimeTypes.APPLICATION_TTML) + .setLanguage("en") + .setSelectionFlags(C.SELECTION_FLAG_FORCED) + .setRoleFlags(C.ROLE_FLAG_ALTERNATE) + .setLabel("label") + .build())) .setTag(new Object()) .build(); @@ -584,13 +592,13 @@ public class MediaItemTest { .build()) .setSubtitles( ImmutableList.of( - new MediaItem.Subtitle( - Uri.parse(URI_STRING + "/en"), - MimeTypes.APPLICATION_TTML, - /* language= */ "en", - C.SELECTION_FLAG_FORCED, - C.ROLE_FLAG_ALTERNATE, - "label"))) + new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/en")) + .setMimeType(MimeTypes.APPLICATION_TTML) + .setLanguage("en") + .setSelectionFlags(C.SELECTION_FLAG_FORCED) + .setRoleFlags(C.ROLE_FLAG_ALTERNATE) + .setLabel("label") + .build())) .setTag(new Object()) .build();