# MediaSourceEventListener

Artifact: [androidx.media3:media3-exoplayer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/exoplayer/source/MediaSourceEventListener.java+class:androidx.media3.exoplayer.source.MediaSourceEventListener)

*** ** * ** ***


```
@UnstableApi
public interface MediaSourceEventListener
```

<br />

Known direct subclasses [AnalyticsCollector](https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsCollector), [MediaSourceTestRunner.MediaSourceListener](https://developer.android.com/reference/androidx/media3/test/utils/MediaSourceTestRunner.MediaSourceListener), [ServerSideAdInsertionMediaSource](https://developer.android.com/reference/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource)

|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsCollector` | Interface for data collectors that forward analytics events to `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener`. |
| `https://developer.android.com/reference/androidx/media3/test/utils/MediaSourceTestRunner.MediaSourceListener` |   |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource` | A `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource` for server-side inserted ad breaks. |

Known indirect subclasses [DefaultAnalyticsCollector](https://developer.android.com/reference/androidx/media3/exoplayer/analytics/DefaultAnalyticsCollector)

|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/DefaultAnalyticsCollector` | Data collector that forwards analytics events to `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener`. |

*** ** * ** ***

Interface for callbacks to be notified of `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource` events.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener.EventDispatcher` Dispatches events to `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener`. |

| ### Public methods |
|---|---|
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onDownstreamFormatChanged(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.MediaLoadData)( int windowIndex, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData )` Called when a downstream format change occurs (i.e. when the format of the media being read from one or more `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream`s provided by the source changes). |
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadCanceled(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData)( int windowIndex, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData )` Called when a load is canceled. |
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadCompleted(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData)( int windowIndex, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData )` Called when a load ends. |
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadError(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData,java.io.IOException,boolean)( int windowIndex, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData, https://developer.android.com/reference/java/io/IOException.html error, boolean wasCanceled )` Called when a load error occurs. |
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadStarted(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData,int)( int windowIndex, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData, int retryCount )` Called when a load begins. |
| `default void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onUpstreamDiscarded(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.MediaLoadData)( int windowIndex, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId, https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData )` Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format. |

## Public methods

### onDownstreamFormatChanged

```
default void onDownstreamFormatChanged(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    MediaLoadData mediaLoadData
)
```

Called when a downstream format change occurs (i.e. when the format of the media being read from one or more `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream`s provided by the source changes).

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` the media belongs to. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the newly selected downstream data. |

### onLoadCanceled

```
default void onLoadCanceled(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)
```

Called when a load is canceled.

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo` corresponding to the event. The values of `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#elapsedRealtimeMs()` and `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#bytesLoaded()` are relative to the corresponding `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadStarted(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData,int)` event. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the data being loaded. |

### onLoadCompleted

```
default void onLoadCompleted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)
```

Called when a load ends.

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo` corresponding to the event. The values of `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#elapsedRealtimeMs()` and `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#bytesLoaded()` are relative to the corresponding `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadStarted(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData,int)` event. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the data being loaded. |

### onLoadError

```
default void onLoadError(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled
)
```

Called when a load error occurs.

The error may or may not have resulted in the load being canceled, as indicated by the `wasCanceled` parameter. If the load was canceled, `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadCanceled(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData)` will *not* be called in addition to this method.

This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should *not* implement this method to display a user visible error or initiate an application level retry. `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onPlayerError(androidx.media3.common.PlaybackException)` is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo` corresponding to the event. The values of `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#elapsedRealtimeMs()` and `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#bytesLoaded()` are relative to the corresponding `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener#onLoadStarted(int,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.exoplayer.source.LoadEventInfo,androidx.media3.exoplayer.source.MediaLoadData,int)` event. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the data being loaded. |
| `https://developer.android.com/reference/java/io/IOException.html error` | The load error. |
| `boolean wasCanceled` | Whether the load was canceled as a result of the error. |

### onLoadStarted

```
default void onLoadStarted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    int retryCount
)
```

Called when a load begins.

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo loadEventInfo` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo` corresponding to the event. The value of `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#uri()` won't reflect potential redirection yet and `https://developer.android.com/reference/androidx/media3/exoplayer/source/LoadEventInfo#responseHeaders()` will be empty. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the data being loaded. |
| `int retryCount` | The number of failed attempts since this method was first called (this is zero for the first load attempt). |

### onUpstreamDiscarded

```
default void onUpstreamDiscarded(
    int windowIndex,
    MediaSource.MediaPeriodId mediaPeriodId,
    MediaLoadData mediaLoadData
)
```

Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format.

| Parameters |
|---|---|
| `int windowIndex` | The window index in the timeline of the media source this load belongs to. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId mediaPeriodId` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` the media belongs to. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData mediaLoadData` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaLoadData` defining the media being discarded. |