diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 7646e4f42a..1b333bc9ca 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -74,6 +74,8 @@ * Assume Dolby Vision content is encoded as H264 when calculating maximum codec input size ([#8705](https://github.com/google/ExoPlayer/issues/8705)). + * Use an empty string instead of the URI if the media ID is not explicitly + set with `MediaItem.Builder.setMediaId(String)`. * HLS: * Fix bug of ignoring `EXT-X-START` when setting the live target offset ([#8764](https://github.com/google/ExoPlayer/pull/8764)). diff --git a/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/DefaultMediaItemConverter.java b/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/DefaultMediaItemConverter.java index e6d4550d88..57b5ac8b48 100644 --- a/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/DefaultMediaItemConverter.java +++ b/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/DefaultMediaItemConverter.java @@ -84,7 +84,7 @@ public class DefaultMediaItemConverter implements MediaItemConverter { return new MediaItem.Builder() .setUri(uri) - .setMediaId(mediaId) + .setMediaId(mediaId != null ? mediaId : MediaItem.DEFAULT_MEDIA_ID) .setMediaMetadata( new com.google.android.exoplayer2.MediaMetadata.Builder().setTitle(title).build()) .setTag(media2MediaItem) 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 68cf30d18f..8e9ecb27d1 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 @@ -147,17 +147,17 @@ public final class MediaItem implements Bundleable { } /** - * Sets the optional media ID which identifies the media item. If not specified, {@link #setUri} - * must be called and the string representation of {@link PlaybackProperties#uri} is used as the - * media ID. + * Sets the optional media ID which identifies the media item. + * + *

By default {@link #DEFAULT_MEDIA_ID} is used. */ - public Builder setMediaId(@Nullable String mediaId) { - this.mediaId = mediaId; + public Builder setMediaId(String mediaId) { + this.mediaId = checkNotNull(mediaId); return this; } /** - * Sets the optional URI. If not specified, {@link #setMediaId(String)} must be called. + * Sets the optional URI. * *

If {@code uri} is null or unset no {@link PlaybackProperties} object is created during * {@link #build()} and any other {@code Builder} methods that would populate {@link @@ -168,7 +168,7 @@ public final class MediaItem implements Bundleable { } /** - * Sets the optional URI. If not specified, {@link #setMediaId(String)} must be called. + * Sets the optional URI. * *

If {@code uri} is null or unset no {@link PlaybackProperties} object is created during * {@link #build()} and any other {@code Builder} methods that would populate {@link @@ -587,10 +587,9 @@ public final class MediaItem implements Bundleable { customCacheKey, subtitles, tag); - mediaId = mediaId != null ? mediaId : uri.toString(); } return new MediaItem( - checkNotNull(mediaId), + mediaId != null ? mediaId : DEFAULT_MEDIA_ID, new ClippingProperties( clipStartPositionMs, clipEndPositionMs, @@ -1194,6 +1193,12 @@ public final class MediaItem implements Bundleable { } } + /** + * The default media ID that is used if the media ID is not explicitly set by {@link + * Builder#setMediaId(String)}. + */ + public static final String DEFAULT_MEDIA_ID = ""; + /** Identifies the media item. */ public final String mediaId; @@ -1296,7 +1301,7 @@ public final class MediaItem implements Bundleable { public static final Creator CREATOR = MediaItem::fromBundle; private static MediaItem fromBundle(Bundle bundle) { - String mediaId = checkNotNull(bundle.getString(keyForField(FIELD_MEDIA_ID))); + String mediaId = checkNotNull(bundle.getString(keyForField(FIELD_MEDIA_ID), DEFAULT_MEDIA_ID)); @Nullable Bundle liveConfigurationBundle = bundle.getBundle(keyForField(FIELD_LIVE_CONFIGURATION)); LiveConfiguration liveConfiguration; 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 e50ac6ea08..0654eb1c70 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 @@ -37,19 +37,13 @@ public class MediaItemTest { private static final String URI_STRING = "http://www.google.com"; - @Test - public void builder_needsUriOrMediaId() { - assertThrows(NullPointerException.class, () -> new MediaItem.Builder().build()); - } - @Test public void builderWithUri_setsUri() { Uri uri = Uri.parse(URI_STRING); MediaItem mediaItem = MediaItem.fromUri(uri); - assertThat(mediaItem.playbackProperties.uri.toString()).isEqualTo(URI_STRING); - assertThat(mediaItem.mediaId).isEqualTo(URI_STRING); + assertThat(mediaItem.playbackProperties.uri).isEqualTo(uri); assertThat(mediaItem.mediaMetadata).isNotNull(); } @@ -58,7 +52,13 @@ public class MediaItemTest { MediaItem mediaItem = MediaItem.fromUri(URI_STRING); assertThat(mediaItem.playbackProperties.uri.toString()).isEqualTo(URI_STRING); - assertThat(mediaItem.mediaId).isEqualTo(URI_STRING); + } + + @Test + public void builderWithoutMediaId_usesDefaultMediaId() { + MediaItem mediaItem = MediaItem.fromUri(URI_STRING); + + assertThat(mediaItem.mediaId).isEqualTo(MediaItem.DEFAULT_MEDIA_ID); } @Test