Package com.google.android.exoplayer2

## Class MediaItem.Builder

- java.lang.Object
- - com.google.android.exoplayer2.MediaItem.Builder

- Enclosing class:
    MediaItem

      public static final class MediaItem.Builder
      extends Object

  A builder for `MediaItem` instances.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `Builder()` | Creates a builder.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `MediaItem` | `build()` | Returns a new `MediaItem` instance with the current builder values.
    `MediaItem.Builder` | `setAdTagUri​(Uri adTagUri)` | Sets the optional ad tag `Uri`.
    `MediaItem.Builder` | `setAdTagUri​(Uri adTagUri, Object adsId)` | Sets the optional ad tag `Uri` and ads identifier.
    `MediaItem.Builder` | `setAdTagUri​(String adTagUri)` | Sets the optional ad tag `Uri`.
    `MediaItem.Builder` | `setClipEndPositionMs​(long endPositionMs)` | Sets the optional end position in milliseconds which must be a value larger than or equal to zero, or `C.TIME_END_OF_SOURCE` to end when playback reaches the end of media (Default: `C.TIME_END_OF_SOURCE`).
    `MediaItem.Builder` | `setClipRelativeToDefaultPosition​(boolean relativeToDefaultPosition)` | Sets whether the start position and the end position are relative to the default position in the window (Default: `false`).
    `MediaItem.Builder` | `setClipRelativeToLiveWindow​(boolean relativeToLiveWindow)` | Sets whether the start/end positions should move with the live window for live streams.
    `MediaItem.Builder` | `setClipStartPositionMs​(long startPositionMs)` | Sets the optional start position in milliseconds which must be a value larger than or equal to zero (Default: 0).
    `MediaItem.Builder` | `setClipStartsAtKeyFrame​(boolean startsAtKeyFrame)` | Sets whether the start point is guaranteed to be a key frame.
    `MediaItem.Builder` | `setCustomCacheKey​(String customCacheKey)` | Sets the optional custom cache key (only used for progressive streams).
    `MediaItem.Builder` | `setDrmForceDefaultLicenseUri​(boolean forceDefaultLicenseUri)` | Sets whether to force use the default DRM license server URI even if the media specifies its own DRM license server URI.
    `MediaItem.Builder` | `setDrmKeySetId​(byte[] keySetId)` | Sets the key set ID of the offline license.
    `MediaItem.Builder` | `setDrmLicenseRequestHeaders​(Map<String,​String> licenseRequestHeaders)` | Sets the optional request headers attached to the DRM license request.
    `MediaItem.Builder` | `setDrmLicenseUri​(Uri licenseUri)` | Sets the optional default DRM license server URI.
    `MediaItem.Builder` | `setDrmLicenseUri​(String licenseUri)` | Sets the optional default DRM license server URI.
    `MediaItem.Builder` | `setDrmMultiSession​(boolean multiSession)` | Sets whether the DRM configuration is multi session enabled.
    `MediaItem.Builder` | `setDrmPlayClearContentWithoutKey​(boolean playClearContentWithoutKey)` | Sets whether clear samples within protected content should be played when keys for the encrypted part of the content have yet to be loaded.
    `MediaItem.Builder` | `setDrmSessionForClearPeriods​(boolean sessionForClearPeriods)` | Sets whether a DRM session should be used for clear tracks of type `C.TRACK_TYPE_VIDEO` and `C.TRACK_TYPE_AUDIO`.
    `MediaItem.Builder` | `setDrmSessionForClearTypes​(List<Integer> sessionForClearTypes)` | Sets a list of `C``.TRACK_TYPE_*` constants for which to use a DRM session even when the tracks are in the clear.
    `MediaItem.Builder` | `setDrmUuid​(UUID uuid)` | Sets the `UUID` of the protection scheme.
    `MediaItem.Builder` | `setLiveMaxOffsetMs​(long liveMaxOffsetMs)` | Sets the optional maximum offset from the live edge for live streams, in milliseconds.
    `MediaItem.Builder` | `setLiveMaxPlaybackSpeed​(float maxPlaybackSpeed)` | Sets the optional maximum playback speed for live stream speed adjustment.
    `MediaItem.Builder` | `setLiveMinOffsetMs​(long liveMinOffsetMs)` | Sets the optional minimum offset from the live edge for live streams, in milliseconds.
    `MediaItem.Builder` | `setLiveMinPlaybackSpeed​(float minPlaybackSpeed)` | Sets the optional minimum playback speed for live stream speed adjustment.
    `MediaItem.Builder` | `setLiveTargetOffsetMs​(long liveTargetOffsetMs)` | Sets the optional target offset from the live edge for live streams, in milliseconds.
    `MediaItem.Builder` | `setMediaId​(String mediaId)` | Sets the optional media ID which identifies the media item.
    `MediaItem.Builder` | `setMediaMetadata​(MediaMetadata mediaMetadata)` | Sets the media metadata.
    `MediaItem.Builder` | `setMimeType​(String mimeType)` | Sets the optional MIME type.
    `MediaItem.Builder` | `setStreamKeys​(List<StreamKey> streamKeys)` | Sets the optional stream keys by which the manifest is filtered (only used for adaptive streams).
    `MediaItem.Builder` | `setSubtitles​(List<MediaItem.Subtitle> subtitles)` | Sets the optional subtitles.
    `MediaItem.Builder` | `setTag​(Object tag)` | Sets the optional tag for custom attributes.
    `MediaItem.Builder` | `setUri​(Uri uri)` | Sets the optional URI.
    `MediaItem.Builder` | `setUri​(String uri)` | Sets the optional URI.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Builder

          public Builder()

      Creates a builder.

  - ### Method Detail

    - #### setMediaId

          public MediaItem.Builder setMediaId​(String mediaId)

      Sets the optional media ID which identifies the media item.

      By default `MediaItem.DEFAULT_MEDIA_ID` is used.

    - #### setUri

          public MediaItem.Builder setUri​(@Nullable
                                          String uri)

      Sets the optional URI.

      If `uri` is null or unset then no `MediaItem.PlaybackProperties` object is created during `build()` and no other `Builder` methods that would populate `MediaItem.playbackProperties` should be called.

    - #### setUri

          public MediaItem.Builder setUri​(@Nullable
                                          Uri uri)

      Sets the optional URI.

      If `uri` is null or unset then no `MediaItem.PlaybackProperties` object is created during `build()` and no other `Builder` methods that would populate `MediaItem.playbackProperties` should be called.

    - #### setMimeType

          public MediaItem.Builder setMimeType​(@Nullable
                                               String mimeType)

      Sets the optional MIME type.

      The MIME type may be used as a hint for inferring the type of the media item.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

      Parameters:
        `mimeType` - The MIME type.

    - #### setClipStartPositionMs

          public MediaItem.Builder setClipStartPositionMs​(long startPositionMs)

      Sets the optional start position in milliseconds which must be a value larger than or equal to zero (Default: 0).

    - #### setClipEndPositionMs

          public MediaItem.Builder setClipEndPositionMs​(long endPositionMs)

      Sets the optional end position in milliseconds which must be a value larger than or equal to zero, or `C.TIME_END_OF_SOURCE` to end when playback reaches the end of media (Default: `C.TIME_END_OF_SOURCE`).

    - #### setClipRelativeToLiveWindow

          public MediaItem.Builder setClipRelativeToLiveWindow​(boolean relativeToLiveWindow)

      Sets whether the start/end positions should move with the live window for live streams. If `false`, live streams end when playback reaches the end position in live window seen when the media is first loaded (Default: `false`).

    - #### setClipRelativeToDefaultPosition

          public MediaItem.Builder setClipRelativeToDefaultPosition​(boolean relativeToDefaultPosition)

      Sets whether the start position and the end position are relative to the default position in the window (Default: `false`).

    - #### setClipStartsAtKeyFrame

          public MediaItem.Builder setClipStartsAtKeyFrame​(boolean startsAtKeyFrame)

      Sets whether the start point is guaranteed to be a key frame. If `false`, the playback transition into the clip may not be seamless (Default: `false`).

    - #### setDrmLicenseUri

          public MediaItem.Builder setDrmLicenseUri​(@Nullable
                                                    Uri licenseUri)

      Sets the optional default DRM license server URI. If this URI is set, the `MediaItem.DrmConfiguration.uuid` needs to be specified as well.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmLicenseUri

          public MediaItem.Builder setDrmLicenseUri​(@Nullable
                                                    String licenseUri)

      Sets the optional default DRM license server URI. If this URI is set, the `MediaItem.DrmConfiguration.uuid` needs to be specified as well.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmLicenseRequestHeaders

          public MediaItem.Builder setDrmLicenseRequestHeaders​(@Nullable
                                                               Map<String,​String> licenseRequestHeaders)

      Sets the optional request headers attached to the DRM license request.

      `null` or an empty `Map` can be used for a reset.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmUuid

          public MediaItem.Builder setDrmUuid​(@Nullable
                                              UUID uuid)

      Sets the `UUID` of the protection scheme.

      If `uuid` is null or unset then no `MediaItem.DrmConfiguration` object is created during `build()` and no other `Builder` methods that would populate `MediaItem.PlaybackProperties.drmConfiguration` should be called.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

    - #### setDrmMultiSession

          public MediaItem.Builder setDrmMultiSession​(boolean multiSession)

      Sets whether the DRM configuration is multi session enabled.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmForceDefaultLicenseUri

          public MediaItem.Builder setDrmForceDefaultLicenseUri​(boolean forceDefaultLicenseUri)

      Sets whether to force use the default DRM license server URI even if the media specifies its own DRM license server URI.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmPlayClearContentWithoutKey

          public MediaItem.Builder setDrmPlayClearContentWithoutKey​(boolean playClearContentWithoutKey)

      Sets whether clear samples within protected content should be played when keys for the encrypted part of the content have yet to be loaded.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmSessionForClearPeriods

          public MediaItem.Builder setDrmSessionForClearPeriods​(boolean sessionForClearPeriods)

      Sets whether a DRM session should be used for clear tracks of type `C.TRACK_TYPE_VIDEO` and `C.TRACK_TYPE_AUDIO`.

      This method overrides what has been set by previously calling `setDrmSessionForClearTypes(List)`.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmSessionForClearTypes

          public MediaItem.Builder setDrmSessionForClearTypes​(@Nullable
                                                              List<Integer> sessionForClearTypes)

      Sets a list of `C``.TRACK_TYPE_*` constants for which to use a DRM session even when the tracks are in the clear.

      For the common case of using a DRM session for `C.TRACK_TYPE_VIDEO` and `C.TRACK_TYPE_AUDIO` the `setDrmSessionForClearPeriods(boolean)` can be used.

      This method overrides what has been set by previously calling `setDrmSessionForClearPeriods(boolean)`.

      `null` or an empty `List` can be used for a reset.

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setDrmKeySetId

          public MediaItem.Builder setDrmKeySetId​(@Nullable
                                                  byte[] keySetId)

      Sets the key set ID of the offline license.

      The key set ID identifies an offline license. The ID is required to query, renew or release an existing offline license (see `DefaultDrmSessionManager#setMode(int mode,byte[] offlineLicenseKeySetId)`).

      This method should only be called if both `setUri(java.lang.String)` and `setDrmUuid(UUID)` are passed non-null values.

    - #### setStreamKeys

          public MediaItem.Builder setStreamKeys​(@Nullable
                                                 List<StreamKey> streamKeys)

      Sets the optional stream keys by which the manifest is filtered (only used for adaptive streams).

      `null` or an empty `List` can be used for a reset.

      If `setUri(java.lang.String)` is passed a non-null `uri`, the stream keys are used to create a `MediaItem.PlaybackProperties` object. Otherwise they will be ignored.

    - #### setCustomCacheKey

          public MediaItem.Builder setCustomCacheKey​(@Nullable
                                                     String customCacheKey)

      Sets the optional custom cache key (only used for progressive streams).

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

    - #### setSubtitles

          public MediaItem.Builder setSubtitles​(@Nullable
                                                List<MediaItem.Subtitle> subtitles)

      Sets the optional subtitles.

      `null` or an empty `List` can be used for a reset.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

    - #### setAdTagUri

          public MediaItem.Builder setAdTagUri​(@Nullable
                                               String adTagUri)

      Sets the optional ad tag `Uri`.

      Media items in the playlist with the same ad tag URI, media ID and ads loader will share the same ad playback state. To resume ad playback when recreating the playlist on returning from the background, pass media items with the same ad tag URIs and media IDs to the player.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

      Parameters:
        `adTagUri` - The ad tag URI to load.

    - #### setAdTagUri

          public MediaItem.Builder setAdTagUri​(@Nullable
                                               Uri adTagUri)

      Sets the optional ad tag `Uri`.

      Media items in the playlist with the same ad tag URI, media ID and ads loader will share the same ad playback state. To resume ad playback when recreating the playlist on returning from the background, pass media items with the same ad tag URIs and media IDs to the player.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

      Parameters:
        `adTagUri` - The ad tag URI to load.

    - #### setAdTagUri

          public MediaItem.Builder setAdTagUri​(@Nullable
                                               Uri adTagUri,
                                               @Nullable
                                               Object adsId)

      Sets the optional ad tag `Uri` and ads identifier.

      Media items in the playlist that have the same ads identifier and ads loader share the same ad playback state. To resume ad playback when recreating the playlist on returning from the background, pass the same ads IDs to the player.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

      Parameters:
        `adTagUri` - The ad tag URI to load.
        `adsId` - An opaque identifier for ad playback state associated with this item. Ad loading and playback state is shared among all media items that have the same ads ID (by `equality`) and ads loader, so it is important to pass the same identifiers when constructing playlist items each time the player returns to the foreground.

    - #### setLiveTargetOffsetMs

          public MediaItem.Builder setLiveTargetOffsetMs​(long liveTargetOffsetMs)

      Sets the optional target offset from the live edge for live streams, in milliseconds.

      See `Player#getCurrentLiveOffset()`.

      Parameters:
        `liveTargetOffsetMs` - The target offset, in milliseconds, or `C.TIME_UNSET` to use the media-defined default.

    - #### setLiveMinOffsetMs

          public MediaItem.Builder setLiveMinOffsetMs​(long liveMinOffsetMs)

      Sets the optional minimum offset from the live edge for live streams, in milliseconds.

      See `Player#getCurrentLiveOffset()`.

      Parameters:
        `liveMinOffsetMs` - The minimum allowed offset, in milliseconds, or `C.TIME_UNSET` to use the media-defined default.

    - #### setLiveMaxOffsetMs

          public MediaItem.Builder setLiveMaxOffsetMs​(long liveMaxOffsetMs)

      Sets the optional maximum offset from the live edge for live streams, in milliseconds.

      See `Player#getCurrentLiveOffset()`.

      Parameters:
        `liveMaxOffsetMs` - The maximum allowed offset, in milliseconds, or `C.TIME_UNSET` to use the media-defined default.

    - #### setLiveMinPlaybackSpeed

          public MediaItem.Builder setLiveMinPlaybackSpeed​(float minPlaybackSpeed)

      Sets the optional minimum playback speed for live stream speed adjustment.

      This value is ignored for other stream types.

      Parameters:
        `minPlaybackSpeed` - The minimum factor by which playback can be sped up for live streams, or `C.RATE_UNSET` to use the media-defined default.

    - #### setLiveMaxPlaybackSpeed

          public MediaItem.Builder setLiveMaxPlaybackSpeed​(float maxPlaybackSpeed)

      Sets the optional maximum playback speed for live stream speed adjustment.

      This value is ignored for other stream types.

      Parameters:
        `maxPlaybackSpeed` - The maximum factor by which playback can be sped up for live streams, or `C.RATE_UNSET` to use the media-defined default.

    - #### setTag

          public MediaItem.Builder setTag​(@Nullable
                                          Object tag)

      Sets the optional tag for custom attributes. The tag for the media source which will be published in the `com.google.android.exoplayer2.Timeline` of the source as `com.google.android.exoplayer2.Timeline.Window#tag`.

      This method should only be called if `setUri(java.lang.String)` is passed a non-null value.

    - #### setMediaMetadata

          public MediaItem.Builder setMediaMetadata​(MediaMetadata mediaMetadata)

      Sets the media metadata.

    - #### build

          public MediaItem build()

      Returns a new `MediaItem` instance with the current builder values.