ClippingMediaSource


@UnstableApi
public final class ClippingMediaSource extends WrappingMediaSource


MediaSource that wraps a source and clips its timeline based on specified start/end positions. The wrapped source must consist of a single period.

Summary

Nested types

Thrown when a ClippingMediaSource cannot clip its wrapped source.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
public annotation ClippingMediaSource.IllegalClippingException.Reason

The reason clipping failed.

Public constructors

ClippingMediaSource(MediaSource mediaSource, long durationUs)

Creates a new clipping source that wraps the specified source and provides samples from the default position for the specified duration.

ClippingMediaSource(
    MediaSource mediaSource,
    long startPositionUs,
    long endPositionUs
)

Creates a new clipping source that wraps the specified source and provides samples between the specified start and end position.

ClippingMediaSource(
    MediaSource mediaSource,
    long startPositionUs,
    long endPositionUs,
    boolean enableInitialDiscontinuity,
    boolean allowDynamicClippingUpdates,
    boolean relativeToDefaultPosition
)

Creates a new clipping source that wraps the specified source.

Public methods

boolean

Returns whether the for this source can be updated with the provided item.

MediaPeriod
createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

Creates the requested MediaPeriod.

void

Throws any pending error encountered while loading or refreshing source information.

void

Releases a MediaPeriod.

Protected methods

void

Called when the child source info has been refreshed.

void

Releases the source, see releaseSource.

Inherited methods

From androidx.media3.exoplayer.source.BaseMediaSource
final void
addDrmEventListener(
    Handler handler,
    DrmSessionEventListener eventListener
)

Adds a DrmSessionEventListener to the list of listeners which are notified of DRM events for this media source.

final void
addEventListener(
    Handler handler,
    MediaSourceEventListener eventListener
)

Adds a MediaSourceEventListener to the list of listeners which are notified of media source events.

final DrmSessionEventListener.EventDispatcher

Returns a DrmSessionEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified MediaPeriodId

final DrmSessionEventListener.EventDispatcher
createDrmEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Returns a DrmSessionEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified window index and MediaPeriodId.

final MediaSourceEventListener.EventDispatcher

Returns a MediaSourceEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified MediaPeriodId.

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    MediaSource.MediaPeriodId mediaPeriodId,
    long mediaTimeOffsetMs
)

This method is deprecated.

Use createEventDispatcher instead.

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Returns a MediaSourceEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified window index and MediaPeriodId.

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    long mediaTimeOffsetMs
)

This method is deprecated.

Use createEventDispatcher instead.

final void

Disables the source for the creation of MediaPeriods.

final void

Enables the source for the creation of MediaPeriods.

final PlayerId

Returns the PlayerId of the player using this media source.

final boolean

Returns whether the source is enabled.

final void
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener
)

This method is deprecated.

Implement prepareSource instead.

final void
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener,
    PlayerId playerId
)

Registers a MediaSourceCaller.

final boolean

Returns whether the source has prepareSource called.

final void

Updates timeline and manifest and notifies all listeners of the update.

final void

Unregisters a caller, and disables and releases the source if no longer required.

final void

Removes a DrmSessionEventListener from the list of listeners which are notified of DRM events for this media source.

final void

Removes a MediaSourceEventListener from the list of listeners which are notified of media source events.

final void

Sets the PlayerId of the player using this media source.

From androidx.media3.exoplayer.source.CompositeMediaSource
final void

Disables a child source.

void

Disables the source, see disable.

final void

Enables a child source.

void

Enables the source, see enable.

@Nullable MediaSource.MediaPeriodId
getMediaPeriodIdForChildMediaPeriodId(
    @UnknownNull T childSourceId,
    MediaSource.MediaPeriodId mediaPeriodId
)

Returns the MediaPeriodId in the composite source corresponding to the specified in a child source.

long
getMediaTimeForChildMediaTime(
    @UnknownNull T childSourceId,
    long mediaTimeMs,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Returns the media time in the MediaPeriod of the composite source corresponding to the specified media time in the MediaPeriod of the child source.

int
getWindowIndexForChildWindowIndex(
    @UnknownNull T childSourceId,
    int windowIndex
)

Returns the window index in the composite source corresponding to the specified window index in a child source.

abstract void
onChildSourceInfoRefreshed(
    @UnknownNull T childSourceId,
    MediaSource mediaSource,
    Timeline newTimeline
)

Called when the source info of a child source has been refreshed.

final void

Prepares a child source.

final void

Releases a child source.

From androidx.media3.exoplayer.source.WrappingMediaSource
final void

Disables the child source.

final void

Enables the child source.

@Nullable Timeline

Returns the initial placeholder timeline that is returned immediately when the real timeline is not yet known, or null to let the player create an initial timeline.

MediaItem

Returns the whose media is provided by the source.

@Nullable MediaSource.MediaPeriodId

Returns the MediaPeriodId in the wrapping source corresponding to the specified in a child source.

final @Nullable MediaSource.MediaPeriodId
getMediaPeriodIdForChildMediaPeriodId(
    Void childSourceId,
    MediaSource.MediaPeriodId mediaPeriodId
)

Returns the MediaPeriodId in the composite source corresponding to the specified in a child source.

long
getMediaTimeForChildMediaTime(
    long mediaTimeMs,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Returns the media time in the MediaPeriod of the wrapping source corresponding to the specified media time in the MediaPeriod of the child source.

final long
getMediaTimeForChildMediaTime(
    Void childSourceId,
    long mediaTimeMs,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Returns the media time in the MediaPeriod of the composite source corresponding to the specified media time in the MediaPeriod of the child source.

int

Returns the window index in the wrapping source corresponding to the specified window index in a child source.

final int
getWindowIndexForChildWindowIndex(Void childSourceId, int windowIndex)

Returns the window index in the composite source corresponding to the specified window index in a child source.

boolean

Returns true if the media source is guaranteed to never have zero or more than one window.

final void
onChildSourceInfoRefreshed(
    Void childSourceId,
    MediaSource mediaSource,
    Timeline newTimeline
)

Called when the source info of a child source has been refreshed.

final void

Prepares the wrapped child source.

void

Starts source preparation and enables the source, see prepareSource.

final void

Starts source preparation and enables the source, see prepareSource.

final void

Releases the child source.

void

Updates the for this source.

Public constructors

ClippingMediaSource

public ClippingMediaSource(MediaSource mediaSource, long durationUs)

Creates a new clipping source that wraps the specified source and provides samples from the default position for the specified duration.

Parameters
MediaSource mediaSource

The single-period source to wrap.

long durationUs

The duration from the default position in the window in mediaSource's timeline at which to stop providing samples. Specifying a duration that exceeds the mediaSource's duration will result in the end of the source not being clipped.

ClippingMediaSource

public ClippingMediaSource(
    MediaSource mediaSource,
    long startPositionUs,
    long endPositionUs
)

Creates a new clipping source that wraps the specified source and provides samples between the specified start and end position.

Parameters
MediaSource mediaSource

The single-period source to wrap.

long startPositionUs

The start position within mediaSource's window at which to start providing samples, in microseconds.

long endPositionUs

The end position within mediaSource's window at which to stop providing samples, in microseconds. Specify TIME_END_OF_SOURCE to provide samples from the specified start point up to the end of the source. Specifying a position that exceeds the mediaSource's duration will also result in the end of the source not being clipped.

ClippingMediaSource

public ClippingMediaSource(
    MediaSource mediaSource,
    long startPositionUs,
    long endPositionUs,
    boolean enableInitialDiscontinuity,
    boolean allowDynamicClippingUpdates,
    boolean relativeToDefaultPosition
)

Creates a new clipping source that wraps the specified source.

If the start point is guaranteed to be a key frame, pass false to enableInitialPositionDiscontinuity to suppress an initial discontinuity when a period is first read from.

For live streams, if the clipping positions should move with the live window, pass true to allowDynamicClippingUpdates. Otherwise, the live stream ends when the playback reaches endPositionUs in the last reported live window at the time a media period was created.

Parameters
MediaSource mediaSource

The single-period source to wrap.

long startPositionUs

The start position at which to start providing samples, in microseconds. If relativeToDefaultPosition is false, this position is relative to the start of the window in mediaSource's timeline. If relativeToDefaultPosition is true, this position is relative to the default position in the window in mediaSource's timeline.

long endPositionUs

The end position at which to stop providing samples, in microseconds. Specify TIME_END_OF_SOURCE to provide samples from the specified start point up to the end of the source. Specifying a position that exceeds the mediaSource's duration will also result in the end of the source not being clipped. If relativeToDefaultPosition is false, the specified position is relative to the start of the window in mediaSource's timeline. If relativeToDefaultPosition is true, this position is relative to the default position in the window in mediaSource's timeline.

boolean enableInitialDiscontinuity

Whether the initial discontinuity should be enabled.

boolean allowDynamicClippingUpdates

Whether the clipping of active media periods moves with a live window. If false, playback ends when it reaches endPositionUs in the last reported live window at the time a media period was created.

boolean relativeToDefaultPosition

Whether startPositionUs and endPositionUs are relative to the default position in the window in mediaSource's timeline.

Public methods

canUpdateMediaItem

public boolean canUpdateMediaItem(MediaItem mediaItem)

Returns whether the for this source can be updated with the provided item.

Should not be called directly from application code.

This method must be called on the application thread.

This method can be overridden to change whether the MediaItem of the child source can be updated.

createPeriod

public MediaPeriod createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

Creates the requested MediaPeriod.

This method typically forwards to the wrapped media source and optionally wraps the returned MediaPeriod.

See also
createPeriod

maybeThrowSourceInfoRefreshError

public void maybeThrowSourceInfoRefreshError()

Throws any pending error encountered while loading or refreshing source information.

Should not be called directly from application code.

This method must be called on the playback thread and only after prepareSource.

releasePeriod

public void releasePeriod(MediaPeriod mediaPeriod)

Releases a MediaPeriod.

This method typically forwards to the wrapped media source and optionally unwraps the provided MediaPeriod.

See also
releasePeriod

Protected methods

onChildSourceInfoRefreshed

protected void onChildSourceInfoRefreshed(Timeline newTimeline)

Called when the child source info has been refreshed.

This Timeline can be amended if needed, for example using . The Timeline for the wrapping source needs to be published with refreshSourceInfo.

Parameters
Timeline newTimeline

The timeline of the child source.

releaseSourceInternal

protected void releaseSourceInternal()

Releases the source, see releaseSource. This method is called exactly once after each call to prepareSourceInternal.