# SegmentDownloader

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/offline/SegmentDownloader.java+class:androidx.media3.exoplayer.offline.SegmentDownloader)

*** ** * ** ***


```
@UnstableApi
abstract class SegmentDownloader<M : FilterableManifest<M!>?> : Downloader
```

<br />

Known direct subclasses [DashDownloader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/offline/DashDownloader), [HlsDownloader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/hls/offline/HlsDownloader), [SsDownloader](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/dash/offline/DashDownloader` | A downloader for DASH streams. |
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/hls/offline/HlsDownloader` | A downloader for HLS streams. |
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader` | A downloader for SmoothStreaming streams. |

*** ** * ** ***

Base class for multi segment stream downloaders.

| Parameters |
|---|---|
| `<M : https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/FilterableManifest<M!>?>` | The type of the manifest object. |

## Summary

| ### Nested types |
|---|
| `protected abstract class https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.BaseFactory<M : https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/FilterableManifest<M!>?>` A base class of the factory of the concrete extension of `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader`. |
| `protected class https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-comparable/index.html<https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment!>` Smallest unit of content to be downloaded. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS() = 20000` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#SegmentDownloader(androidx.media3.common.MediaItem,androidx.media3.exoplayer.upstream.ParsingLoadable.Parser<M>,androidx.media3.datasource.cache.CacheDataSource.Factory,java.util.concurrent.Executor,long,long,long)( mediaItem: https://developer.android.com/reference/kotlin/androidx/media3/common/MediaItem!, manifestParser: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M!>!, cacheDataSourceFactory: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource.Factory!, executor: https://developer.android.com/reference/java/util/concurrent/Executor.html!, maxMergedSegmentStartTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, startPositionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, durationUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html )` |

| ### Protected constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#SegmentDownloader(androidx.media3.common.MediaItem,androidx.media3.exoplayer.upstream.ParsingLoadable.Parser<M>,androidx.media3.datasource.cache.CacheDataSource.Factory,java.util.concurrent.Executor,long)( mediaItem: https://developer.android.com/reference/kotlin/androidx/media3/common/MediaItem!, manifestParser: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M!>!, cacheDataSourceFactory: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource.Factory!, executor: https://developer.android.com/reference/java/util/concurrent/Executor.html!, maxMergedSegmentStartTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html )` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#cancel()()` Permanently cancels the downloading by this downloader. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#download(androidx.media3.exoplayer.offline.Downloader.ProgressListener)(progressListener: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/Downloader.ProgressListener?)` Downloads the content. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#remove()()` Removes the content. |

| ### Protected functions |
|---|---|
| `T!` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#execute(com.google.common.base.Supplier<androidx.media3.common.util.RunnableFutureTask<T,?>>,boolean)( runnable: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/kotlin/androidx/media3/common/util/RunnableFutureTask<T!, *>!>!, removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Executes the provided `https://developer.android.com/reference/kotlin/androidx/media3/common/util/RunnableFutureTask`. |
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#getCompressibleDataSpec(android.net.Uri)(uri: https://developer.android.com/reference/android/net/Uri.html!)` |
| `M!` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#getManifest(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean)(dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!, dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!, removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Loads and parses a manifest. |
| `M!` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#getManifest(androidx.media3.datasource.DataSource,androidx.media3.exoplayer.upstream.ParsingLoadable.Parser<M>,androidx.media3.datasource.DataSpec,boolean)( dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!, manifestParser: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M!>!, dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!, removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Loads and parses a manifest with a different parser than the base parser that parsed the initial manifest. |
| `abstract (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#getSegments(androidx.media3.datasource.DataSource,M,boolean)(dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!, manifest: M!, removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Returns a list of all downloadable `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment`s for a given manifest. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#durationUs()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#startPositionUs()` |

## Constants

### DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS

```
const val DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20000: Long
```

## Public constructors

### SegmentDownloader

```
SegmentDownloader(
    mediaItem: MediaItem!,
    manifestParser: ParsingLoadable.Parser<M!>!,
    cacheDataSourceFactory: CacheDataSource.Factory!,
    executor: Executor!,
    maxMergedSegmentStartTimeDiffMs: Long,
    startPositionUs: Long,
    durationUs: Long
)
```

| Parameters |
|---|---|
| `mediaItem: https://developer.android.com/reference/kotlin/androidx/media3/common/MediaItem!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/MediaItem` to be downloaded. |
| `manifestParser: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M!>!` | A parser for manifests belonging to the media to be downloaded. |
| `cacheDataSourceFactory: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource.Factory!` | A `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource.Factory` for the cache into which the download will be written. |
| `executor: https://developer.android.com/reference/java/util/concurrent/Executor.html!` | An `https://developer.android.com/reference/java/util/concurrent/Executor.html` used to make requests for the media being downloaded. Providing an `https://developer.android.com/reference/java/util/concurrent/Executor.html` that uses multiple threads will speed up the download by allowing parts of it to be executed in parallel. |
| `maxMergedSegmentStartTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The maximum difference of the start time of two segments, up to which the segments (of the same URI) should be merged into a single download segment, in milliseconds. |
| `startPositionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The start position in microseconds that the download should start from. |
| `durationUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The duration in microseconds from the `startPositionUs` to be downloaded, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` if the media should be downloaded to the end. |

## Protected constructors

### SegmentDownloader

```
protected SegmentDownloader(
    mediaItem: MediaItem!,
    manifestParser: ParsingLoadable.Parser<M!>!,
    cacheDataSourceFactory: CacheDataSource.Factory!,
    executor: Executor!,
    maxMergedSegmentStartTimeDiffMs: Long
)
```

## Public functions

### cancel

```
fun cancel(): Unit
```

Permanently cancels the downloading by this downloader. The caller should also interrupt the downloading thread immediately after calling this method.

Once canceled, `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/Downloader#download(androidx.media3.exoplayer.offline.Downloader.ProgressListener)` cannot be called again.

### download

```
fun download(progressListener: Downloader.ProgressListener?): Unit
```

Downloads the content.

If downloading fails, this method can be called again to resume the download. It cannot be called again after the download has been `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/Downloader#cancel()`.

If downloading is canceled whilst this method is executing, then it is expected that it will return reasonably quickly. However, there are no guarantees about how the method will return, meaning that it can return without throwing, or by throwing any of its documented exceptions. The caller must use its own knowledge about whether downloading has been canceled to determine whether this is why the method has returned, rather than relying on the method returning in a particular way.

| Parameters |
|---|---|
| `progressListener: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/Downloader.ProgressListener?` | A listener to receive progress updates, or `null`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If the download failed to complete successfully. |
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the download was interrupted. |
| `https://developer.android.com/reference/java/util/concurrent/CancellationException.html` | If the download was canceled. |

### remove

```
fun remove(): Unit
```

Removes the content.

## Protected functions

### execute

```
protected fun <T : Any?> execute(
    runnable: Supplier<RunnableFutureTask<T!, *>!>!,
    removing: Boolean
): T!
```

Executes the provided `https://developer.android.com/reference/kotlin/androidx/media3/common/util/RunnableFutureTask`.

| Parameters |
|---|---|
| `runnable: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/kotlin/androidx/media3/common/util/RunnableFutureTask<T!, *>!>!` | A supplier for the `https://developer.android.com/reference/kotlin/androidx/media3/common/util/RunnableFutureTask` to execute. |
| `removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the execution is part of the download being removed. |

| Returns |
|---|---|
| `T!` | The result. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread on which the method is called is interrupted. |
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs during execution. |

### getCompressibleDataSpec

```
protected java-static fun getCompressibleDataSpec(uri: Uri!): DataSpec!
```

### getManifest

```
protected fun getManifest(dataSource: DataSource!, dataSpec: DataSpec!, removing: Boolean): M!
```

Loads and parses a manifest.

| Parameters |
|---|---|
| `dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!` | The source to use when loading the manifest. |
| `dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!` | The manifest `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec`. |
| `removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the manifest is being loaded as part of the download being removed. |

| Returns |
|---|---|
| `M!` | The loaded manifest. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread on which the method is called is interrupted. |
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs during execution. |

### getManifest

```
protected fun getManifest(
    dataSource: DataSource!,
    manifestParser: ParsingLoadable.Parser<M!>!,
    dataSpec: DataSpec!,
    removing: Boolean
): M!
```

Loads and parses a manifest with a different parser than the base parser that parsed the initial manifest.

This is useful for cases where the manifest needs to be loaded and parsed as a different type than the base parser. For example, HLS manifests can be parsed as media playlists that inherit variable declarations from the multivariant playlist.

| Parameters |
|---|---|
| `dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!` | The source to use when loading the manifest. |
| `manifestParser: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M!>!` | The parser to use when parsing the manifest. |
| `dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!` | The manifest `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec`. |
| `removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the manifest is being loaded as part of the download being removed. |

| Returns |
|---|---|
| `M!` | The loaded manifest. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread on which the method is called is interrupted. |
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs during execution. |

### getSegments

```
protected abstract fun getSegments(dataSource: DataSource!, manifest: M!, removing: Boolean): (Mutable)List<SegmentDownloader.Segment!>!
```

Returns a list of all downloadable `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment`s for a given manifest. Any required data should be loaded using `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#getManifest(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean)` or `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader#execute(com.google.common.base.Supplier<androidx.media3.common.util.RunnableFutureTask<T,?>>,boolean)`.

| Parameters |
|---|---|
| `dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource!` | The `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSource` through which to load any required data. |
| `manifest: M!` | The manifest containing the segments. |
| `removing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the segments are being obtained as part of a removal. If true then a partial segment list is returned in the case that a load error prevents all segments from being listed. If false then an `https://developer.android.com/reference/java/io/IOException.html` will be thrown in this case. |

| Returns |
|---|---|
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment!>!` | The list of downloadable `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/offline/SegmentDownloader.Segment`s. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | Thrown if `allowPartialIndex` is false and an execution error occurs, or if the media is not in a form that allows for its segments to be listed. |
| `https://developer.android.com/reference/java/lang/InterruptedException.html` |   |

## Public properties

### durationUs

```
val durationUs: Long
```

### startPositionUs

```
val startPositionUs: Long
```