DownloadHelper
@UnstableApi
public final class DownloadHelper
A helper for initializing and removing downloads.
The helper extracts track information from the media, selects tracks for downloading, and creates download requests based on the selected tracks.
A typical usage of DownloadHelper follows these steps:
- Build the helper using a
Factoryinstance. - Prepare the helper using
prepareand wait for the callback. - Optional: Inspect the selected tracks using
getMappedTrackInfoandgetTrackSelections, and make adjustments usingclearTrackSelections,replaceTrackSelectionsandaddTrackSelection. - Create a download request for the selected track using
getDownloadRequest. - Release the helper using
release.
Summary
Nested types |
|---|
public interface DownloadHelper.CallbackA callback to be notified when the |
public final class DownloadHelper.FactoryA factory of |
public class DownloadHelper.LiveContentUnsupportedException extends IOExceptionThrown at an attempt to download live content. |
Constants |
|
|---|---|
static final DefaultTrackSelector.Parameters |
Default track selection parameters for downloading. |
static final DefaultTrackSelector.Parameters |
This field is deprecated. Use |
Public constructors |
|---|
DownloadHelper(Creates download helper. |
DownloadHelper(Creates download helper. |
Public methods |
|
|---|---|
void |
addAudioLanguagesToSelection(String[] languages)Convenience method to add selections of tracks for all specified audio languages. |
void |
addTextLanguagesToSelection(Convenience method to add selections of tracks for all specified text languages. |
void |
addTrackSelection(Adds a selection of tracks to be downloaded. |
void |
addTrackSelectionForSingleRenderer(Convenience method to add a selection of tracks to be downloaded for a single renderer. |
void |
clearTrackSelections(int periodIndex)Clears the selection of tracks for a period. |
static MediaSource |
createMediaSource(Equivalent to |
static MediaSource |
createMediaSource(Utility method to create a |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DownloadHelper |
This method is deprecated. Use |
static DefaultTrackSelector.Parameters |
This method is deprecated. Use |
DownloadRequest |
getDownloadRequest(@Nullable byte[] data)Builds a |
DownloadRequest |
getDownloadRequest(String id, @Nullable byte[] data)Builds a |
DownloadRequest |
getDownloadRequest(Builds a |
DownloadRequest |
getDownloadRequest(Builds a |
@Nullable Object |
Returns the manifest, or null if no manifest is loaded. |
MappingTrackSelector.MappedTrackInfo |
getMappedTrackInfo(int periodIndex)Returns the mapped track info for the given period. |
int |
Returns the number of periods for which media is available. |
TrackGroupArray |
getTrackGroups(int periodIndex)Returns the track groups for the given period. |
List<ExoTrackSelection> |
getTrackSelections(int periodIndex, int rendererIndex)Returns all |
Tracks |
getTracks(int periodIndex)Returns |
void |
prepare(DownloadHelper.Callback callback)Initializes the helper for starting a download. |
void |
release()Releases the helper and all resources it is holding. |
void |
replaceTrackSelections(Replaces a selection of tracks to be downloaded. |
Constants
DEFAULT_TRACK_SELECTOR_PARAMETERS
public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS
Default track selection parameters for downloading.
DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT
public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT
Public constructors
DownloadHelper
public DownloadHelper(
MediaItem mediaItem,
@Nullable MediaSource mediaSource,
TrackSelectionParameters trackSelectionParameters,
RendererCapabilitiesList rendererCapabilities
)
Creates download helper.
| Parameters | |
|---|---|
MediaItem mediaItem |
The media item. |
@Nullable MediaSource mediaSource |
A |
TrackSelectionParameters trackSelectionParameters |
|
RendererCapabilitiesList rendererCapabilities |
The |
DownloadHelper
public DownloadHelper(
MediaItem mediaItem,
@Nullable MediaSource mediaSource,
TrackSelectionParameters trackSelectionParameters,
RendererCapabilitiesList rendererCapabilities,
boolean debugLoggingEnabled
)
Creates download helper.
| Parameters | |
|---|---|
MediaItem mediaItem |
The media item. |
@Nullable MediaSource mediaSource |
A |
TrackSelectionParameters trackSelectionParameters |
|
RendererCapabilitiesList rendererCapabilities |
The |
boolean debugLoggingEnabled |
Whether to log debug information. |
Public methods
addAudioLanguagesToSelection
public void addAudioLanguagesToSelection(String[] languages)
Convenience method to add selections of tracks for all specified audio languages. If an audio track in one of the specified languages is not available, the default fallback audio track is used instead. Must not be called until onPrepared is triggered and the passed tracksInfoAvailable is true.
| Parameters | |
|---|---|
String[] languages |
A list of audio languages for which tracks should be added to the download selection, as IETF BCP 47 conformant tags. |
addTextLanguagesToSelection
public void addTextLanguagesToSelection(
boolean selectUndeterminedTextLanguage,
String[] languages
)
Convenience method to add selections of tracks for all specified text languages. Must not be called until onPrepared is triggered and the passed tracksInfoAvailable is true.
| Parameters | |
|---|---|
boolean selectUndeterminedTextLanguage |
Whether a text track with undetermined language should be selected for downloading if no track with one of the specified |
String[] languages |
A list of text languages for which tracks should be added to the download selection, as IETF BCP 47 conformant tags. |
addTrackSelection
public void addTrackSelection(
int periodIndex,
TrackSelectionParameters trackSelectionParameters
)
Adds a selection of tracks to be downloaded. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index this track selection is added for. |
TrackSelectionParameters trackSelectionParameters |
The |
addTrackSelectionForSingleRenderer
public void addTrackSelectionForSingleRenderer(
int periodIndex,
int rendererIndex,
DefaultTrackSelector.Parameters trackSelectorParameters,
List<DefaultTrackSelector.SelectionOverride> overrides
)
Convenience method to add a selection of tracks to be downloaded for a single renderer. Must not be called until onPrepared is triggered and the passed tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index the track selection is added for. |
int rendererIndex |
The renderer index the track selection is added for. |
DefaultTrackSelector.Parameters trackSelectorParameters |
The |
List<DefaultTrackSelector.SelectionOverride> overrides |
A list of |
clearTrackSelections
public void clearTrackSelections(int periodIndex)
Clears the selection of tracks for a period. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index for which track selections are cleared. |
createMediaSource
public static MediaSource createMediaSource(
DownloadRequest downloadRequest,
DataSource.Factory dataSourceFactory
)
Equivalent to createMediaSource(downloadRequest, dataSourceFactory, null).
createMediaSource
public static MediaSource createMediaSource(
DownloadRequest downloadRequest,
DataSource.Factory dataSourceFactory,
@Nullable DrmSessionManager drmSessionManager
)
Utility method to create a MediaSource that only exposes the tracks defined in
downloadRequest.
| Parameters | |
|---|---|
DownloadRequest downloadRequest |
|
DataSource.Factory dataSourceFactory |
A factory for |
@Nullable DrmSessionManager drmSessionManager |
An optional |
| Returns | |
|---|---|
MediaSource |
A |
forMediaItem
public static DownloadHelperforMediaItem(
Context context,
MediaItem mediaItem,
DataSource.Factory dataSourceFactory
)
forMediaItem
public static DownloadHelperforMediaItem(
Context context,
MediaItem mediaItem,
DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled
)
forMediaItem
public static DownloadHelperforMediaItem(
Context context,
MediaItem mediaItem,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory
)
forMediaItem
public static DownloadHelperforMediaItem(
MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory
)
forMediaItem
public static DownloadHelperforMediaItem(
Context context,
MediaItem mediaItem,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled
)
forMediaItem
public static DownloadHelperforMediaItem(
MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled
)
forMediaItem
public static DownloadHelperforMediaItem(
MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory,
@Nullable DrmSessionManager drmSessionManager
)
forMediaItem
public static DownloadHelperforMediaItem(
MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory,
@Nullable DrmSessionManager drmSessionManager,
boolean debugLoggingEnabled
)
getDefaultTrackSelectorParameters
public static DefaultTrackSelector.ParametersgetDefaultTrackSelectorParameters(Context context)
getDownloadRequest
public DownloadRequest getDownloadRequest(@Nullable byte[] data)
Builds a DownloadRequest for downloading the selected tracks. Must not be called until after preparation completes. The uri of the DownloadRequest will be used as content id.
getDownloadRequest
public DownloadRequest getDownloadRequest(String id, @Nullable byte[] data)
Builds a DownloadRequest for downloading the selected tracks. Must not be called until after preparation completes.
getDownloadRequest
public DownloadRequest getDownloadRequest(
@Nullable byte[] data,
long startPositionMs,
long durationMs
)
Builds a DownloadRequest for downloading the selected tracks and time range. Must not be called until preparation completes.
This method is only supported for progressive streams.
| Parameters | |
|---|---|
@Nullable byte[] data |
Application provided data to store in |
long startPositionMs |
The start position (in milliseconds) of the media that download should cover from, or |
long durationMs |
The end position (in milliseconds) of the media that download should cover to, or |
getDownloadRequest
public DownloadRequest getDownloadRequest(
String id,
@Nullable byte[] data,
long startPositionMs,
long durationMs
)
Builds a DownloadRequest for downloading the selected tracks and time range. Must not be called until preparation completes.
This method is only supported for progressive streams.
| Parameters | |
|---|---|
String id |
The unique content id. |
@Nullable byte[] data |
Application provided data to store in |
long startPositionMs |
The start position (in milliseconds) of the media that download should cover from, or |
long durationMs |
The duration (in milliseconds) of the media that download should cover, or |
getManifest
public @Nullable Object getManifest()
Returns the manifest, or null if no manifest is loaded. Must not be called until onPrepared is triggered.
getMappedTrackInfo
public MappingTrackSelector.MappedTrackInfo getMappedTrackInfo(int periodIndex)
Returns the mapped track info for the given period. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index. |
| Returns | |
|---|---|
MappingTrackSelector.MappedTrackInfo |
The |
getPeriodCount
public int getPeriodCount()
Returns the number of periods for which media is available. Must not be called until onPrepared is triggered.
getTrackGroups
public TrackGroupArray getTrackGroups(int periodIndex)
Returns the track groups for the given period. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
Use getMappedTrackInfo to get the track groups mapped to renderers.
| Parameters | |
|---|---|
int periodIndex |
The period index. |
| Returns | |
|---|---|
TrackGroupArray |
The track groups for the period. May be |
getTrackSelections
public List<ExoTrackSelection> getTrackSelections(int periodIndex, int rendererIndex)
Returns all track selections for a period and renderer. Must not be called until onPrepared is triggered and the passed tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index. |
int rendererIndex |
The renderer index. |
| Returns | |
|---|---|
List<ExoTrackSelection> |
A list of selected |
getTracks
public Tracks getTracks(int periodIndex)
Returns Tracks for the given period. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index. |
prepare
public void prepare(DownloadHelper.Callback callback)
Initializes the helper for starting a download.
| Parameters | |
|---|---|
DownloadHelper.Callback callback |
A callback to be notified when preparation completes or fails. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If the download helper has already been prepared. |
replaceTrackSelections
public void replaceTrackSelections(
int periodIndex,
TrackSelectionParameters trackSelectionParameters
)
Replaces a selection of tracks to be downloaded. Must not be called until onPrepared is triggered and the passed
tracksInfoAvailable is true.
| Parameters | |
|---|---|
int periodIndex |
The period index for which the track selection is replaced. |
TrackSelectionParameters trackSelectionParameters |
The |