# 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
public abstract class SegmentDownloader<M extends FilterableManifest<M>> implements Downloader
```

<br />

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

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

*** ** * ** ***

Base class for multi segment stream downloaders.

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

## Summary

| ### Nested types |
|---|
| `protected abstract class https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader.BaseFactory<M extends https://developer.android.com/reference/androidx/media3/exoplayer/offline/FilterableManifest<M>>` A base class of the factory of the concrete extension of `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader`. |
| `protected class https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader.Segment implements https://developer.android.com/reference/java/lang/Comparable.html<https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader.Segment>` Smallest unit of content to be downloaded. |

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS() = 20000` |

| ### Public fields |
|---|---|
| `final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#durationUs()` |
| `final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#startPositionUs()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/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)( https://developer.android.com/reference/androidx/media3/common/MediaItem mediaItem, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M> manifestParser, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource.Factory cacheDataSourceFactory, https://developer.android.com/reference/java/util/concurrent/Executor.html executor, long maxMergedSegmentStartTimeDiffMs, long startPositionUs, long durationUs )` |

| ### Protected constructors |
|---|
| `https://developer.android.com/reference/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)( https://developer.android.com/reference/androidx/media3/common/MediaItem mediaItem, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M> manifestParser, https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource.Factory cacheDataSourceFactory, https://developer.android.com/reference/java/util/concurrent/Executor.html executor, long maxMergedSegmentStartTimeDiffMs )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#cancel()()` Permanently cancels the downloading by this downloader. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#download(androidx.media3.exoplayer.offline.Downloader.ProgressListener)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/offline/Downloader.ProgressListener progressListener)` Downloads the content. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader#remove()()` Removes the content. |

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

## Constants

### DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS

```
public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20000
```

## Public fields

### durationUs

```
public final long durationUs
```

### startPositionUs

```
public final long startPositionUs
```

## Public constructors

### SegmentDownloader

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/MediaItem mediaItem` | The `https://developer.android.com/reference/androidx/media3/common/MediaItem` to be downloaded. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M> manifestParser` | A parser for manifests belonging to the media to be downloaded. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource.Factory cacheDataSourceFactory` | A `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource.Factory` for the cache into which the download will be written. |
| `https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | 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. |
| `long maxMergedSegmentStartTimeDiffMs` | 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. |
| `long startPositionUs` | The start position in microseconds that the download should start from. |
| `long durationUs` | The duration in microseconds from the `startPositionUs` to be downloaded, or `https://developer.android.com/reference/androidx/media3/common/C#TIME_UNSET()` if the media should be downloaded to the end. |

## Protected constructors

### SegmentDownloader

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

## Public methods

### cancel

```
public void cancel()
```

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/androidx/media3/exoplayer/offline/Downloader#download(androidx.media3.exoplayer.offline.Downloader.ProgressListener)` cannot be called again.

### download

```
public final void download(@Nullable Downloader.ProgressListener progressListener)
```

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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/offline/Downloader.ProgressListener 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

```
public final void remove()
```

Removes the content.

## Protected methods

### execute

```
protected final T <T extends Object> execute(
    Supplier<RunnableFutureTask<T, @NonNull ?>> runnable,
    boolean removing
)
```

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

| Parameters |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/androidx/media3/common/util/RunnableFutureTask<T, @https://developer.android.com/reference/androidx/annotation/NonNull ?>> runnable` | A supplier for the `https://developer.android.com/reference/androidx/media3/common/util/RunnableFutureTask` to execute. |
| `boolean removing` | 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 static DataSpec getCompressibleDataSpec(Uri uri)
```

### getManifest

```
protected final M getManifest(DataSource dataSource, DataSpec dataSpec, boolean removing)
```

Loads and parses a manifest.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The source to use when loading the manifest. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | The manifest `https://developer.android.com/reference/androidx/media3/datasource/DataSpec`. |
| `boolean removing` | 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 final M getManifest(
    DataSource dataSource,
    ParsingLoadable.Parser<M> manifestParser,
    DataSpec dataSpec,
    boolean removing
)
```

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 |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The source to use when loading the manifest. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/ParsingLoadable.Parser<M> manifestParser` | The parser to use when parsing the manifest. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | The manifest `https://developer.android.com/reference/androidx/media3/datasource/DataSpec`. |
| `boolean removing` | 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 List<SegmentDownloader.Segment> getSegments(DataSource dataSource, M manifest, boolean removing)
```

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The `https://developer.android.com/reference/androidx/media3/datasource/DataSource` through which to load any required data. |
| `M manifest` | The manifest containing the segments. |
| `boolean removing` | 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://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/offline/SegmentDownloader.Segment>` | The list of downloadable `https://developer.android.com/reference/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` |   |