# ChunkSource

# ChunkSource

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/chunk/ChunkSource.java+class:androidx.media3.exoplayer.source.chunk.ChunkSource)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSource "View this page in Java")

<br />

```
@UnstableApi
interface ChunkSource
```

<br />

Known direct subclasses  
[DashChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/DashChunkSource),[FakeChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeChunkSource),[SsChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/SsChunkSource)  

|-------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [DashChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/DashChunkSource)        | A[ChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource)for DASH streams.                                  |
| [FakeChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeChunkSource)            | Fake[ChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource)with adaptive media chunks of a given duration. |
| [SsChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/SsChunkSource) | A[ChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource)for SmoothStreaming.                               |

Known indirect subclasses  
[DefaultDashChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/DefaultDashChunkSource),[DefaultSsChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/DefaultSsChunkSource)  

|---------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| [DefaultDashChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/DefaultDashChunkSource)        | A default[DashChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/DashChunkSource)implementation.        |
| [DefaultSsChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/DefaultSsChunkSource) | A default[SsChunkSource](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/SsChunkSource)implementation. |

*** ** * ** ***

A provider of[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)s for a[ChunkSampleStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSampleStream)to load.

## Summary

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)       | [getAdjustedSeekPositionUs](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#getAdjustedSeekPositionUs(long,androidx.media3.exoplayer.SeekParameters))`(` ` positionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` seekParameters: `[SeekParameters](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/SeekParameters)`!` `)` Adjusts a seek position given the specified[SeekParameters](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/SeekParameters).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [getNextChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#getNextChunk(androidx.media3.exoplayer.LoadingInfo,long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>,androidx.media3.exoplayer.source.chunk.ChunkHolder))`(` ` loadingInfo: `[LoadingInfo](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/LoadingInfo)`!,` ` loadPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!,` ` out: `[ChunkHolder](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkHolder)`!` `)` Returns the next chunk to load.                                                                                           |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | [getPreferredQueueSize](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#getPreferredQueueSize(long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>))`(` ` playbackPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!` `)` Evaluates whether[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)s should be removed from the back of the queue.                                                                                                                                                                                                                                                                    |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [maybeThrowError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#maybeThrowError())`()` If the source is currently having difficulty providing chunks, then this method throws the underlying error.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onChunkLoadCompleted](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#onChunkLoadCompleted(androidx.media3.exoplayer.source.chunk.Chunk))`(chunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!)` Called when the[ChunkSampleStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSampleStream)has finished loading a chunk obtained from this source.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [onChunkLoadError](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#onChunkLoadError(androidx.media3.exoplayer.source.chunk.Chunk,boolean,androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy.LoadErrorInfo,androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy))`(` ` chunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!,` ` cancelable: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`,` ` loadErrorInfo: `[LoadErrorHandlingPolicy.LoadErrorInfo](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy.LoadErrorInfo)`!,` ` loadErrorHandlingPolicy: `[LoadErrorHandlingPolicy](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy)`!` `)` Called when the[ChunkSampleStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSampleStream)encounters an error loading a chunk obtained from this source. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [release](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#release())`()` Releases any held resources.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [shouldCancelLoad](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#shouldCancelLoad(long,androidx.media3.exoplayer.source.chunk.Chunk,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>))`(` ` playbackPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` loadingChunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!,` ` queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!` `)` Returns whether an ongoing load of a chunk should be canceled.                                                                                                                                                                                                                       |

## Public functions

### getAdjustedSeekPositionUs

```
fun getAdjustedSeekPositionUs(
    positionUs: Long,
    seekParameters: SeekParameters!
): Long
```

Adjusts a seek position given the specified[SeekParameters](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/SeekParameters). Chunk boundaries are used as sync points.  

|                                                           Parameters                                                           |
|--------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------|
| `positionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                    | The seek position in microseconds.                 |
| `seekParameters: `[SeekParameters](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/SeekParameters)`!` | Parameters that control how the seek is performed. |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|----------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The adjusted seek position, in microseconds. |

### getNextChunk

```
fun getNextChunk(
    loadingInfo: LoadingInfo!,
    loadPositionUs: Long,
    queue: (Mutable)List<MediaChunk!>!,
    out: ChunkHolder!
): Unit
```

Returns the next chunk to load.

If a chunk is available then[chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkHolder#chunk())is set. If the end of the stream has been reached then[endOfStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkHolder#endOfStream())is set. If a chunk is not available but the end of the stream has not been reached, the[ChunkHolder](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkHolder)is not modified.  

|                                                                                                                                                            Parameters                                                                                                                                                            |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `loadingInfo: `[LoadingInfo](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/LoadingInfo)`!`                                                                                                                                                                                                            | The[LoadingInfo](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/LoadingInfo)when loading request is made.                                                                                                                                                                      |
| `loadPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                                                  | The current load position in microseconds. If`queue`is empty, this is the starting position from which chunks should be provided. Else it's equal to[endTimeUs](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk#endTimeUs())of the last chunk in the`queue`. |
| `queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!` | The queue of buffered[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)s.                                                                                                                                                                    |
| `out: `[ChunkHolder](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkHolder)`!`                                                                                                                                                                                                       | A holder to populate.                                                                                                                                                                                                                                                                                    |

### getPreferredQueueSize

```
fun getPreferredQueueSize(
    playbackPositionUs: Long,
    queue: (Mutable)List<MediaChunk!>!
): Int
```

Evaluates whether[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)s should be removed from the back of the queue.

Removing[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)s from the back of the queue can be useful if they could be replaced with chunks of a significantly higher quality (e.g. because the available bandwidth has substantially increased).

Will only be called if no[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)in the queue is currently loading.  

|                                                                                                                                                            Parameters                                                                                                                                                            |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `playbackPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                                              | The current playback position, in microseconds.                                                                                       |
| `queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!` | The queue of buffered[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)s. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The preferred queue size. |

### maybeThrowError

```
fun maybeThrowError(): Unit
```

If the source is currently having difficulty providing chunks, then this method throws the underlying error. Otherwise does nothing.  

|                                         Throws                                          |
|-----------------------------------------------------------------------------------------|-----------------------|
| [java.io.IOException](https://developer.android.com/reference/java/io/IOException.html) | The underlying error. |

### onChunkLoadCompleted

```
fun onChunkLoadCompleted(chunk: Chunk!): Unit
```

Called when the[ChunkSampleStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSampleStream)has finished loading a chunk obtained from this source.  

|                                                    Parameters                                                    |
|------------------------------------------------------------------------------------------------------------------|------------------------------------------|
| `chunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!` | The chunk whose load has been completed. |

### onChunkLoadError

```
fun onChunkLoadError(
    chunk: Chunk!,
    cancelable: Boolean,
    loadErrorInfo: LoadErrorHandlingPolicy.LoadErrorInfo!,
    loadErrorHandlingPolicy: LoadErrorHandlingPolicy!
): Boolean
```

Called when the[ChunkSampleStream](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSampleStream)encounters an error loading a chunk obtained from this source.  

|                                                                                      Parameters                                                                                      |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| `chunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!`                                                                     | The chunk whose load encountered the error.                                           |
| `cancelable: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                                    | Whether the load can be canceled.                                                     |
| `loadErrorInfo: `[LoadErrorHandlingPolicy.LoadErrorInfo](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy.LoadErrorInfo)`!` | The load error info.                                                                  |
| `loadErrorHandlingPolicy: `[LoadErrorHandlingPolicy](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy)`!`                   | The load error handling policy to customize the behaviour of handling the load error. |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether the load should be canceled so that a replacement chunk can be loaded instead. Must be`false`if`cancelable`is`false`. If`true`,[getNextChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/ChunkSource#getNextChunk(androidx.media3.exoplayer.LoadingInfo,long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>,androidx.media3.exoplayer.source.chunk.ChunkHolder))will be called to obtain the replacement chunk. |

### release

```
fun release(): Unit
```

Releases any held resources.  

### shouldCancelLoad

```
fun shouldCancelLoad(
    playbackPositionUs: Long,
    loadingChunk: Chunk!,
    queue: (Mutable)List<MediaChunk!>!
): Boolean
```

Returns whether an ongoing load of a chunk should be canceled.  

|                                                                                                                                                            Parameters                                                                                                                                                            |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `playbackPositionUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                                                                                                                                              | The current playback position, in microseconds.                                                                                       |
| `loadingChunk: `[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk)`!`                                                                                                                                                                                                          | The currently loading[Chunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/Chunk).            |
| `queue: (`[Mutable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)`)`[List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[MediaChunk](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk)`!>!` | The queue of buffered[MediaChunks](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/chunk/MediaChunk). |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether the ongoing load of`loadingChunk`should be canceled. |