Class DownloadHelper
- java.lang.Object
-
- com.google.android.exoplayer2.offline.DownloadHelper
-
public final class DownloadHelper extends Object
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 one of the
forMediaItem
methods. - Prepare the helper using
prepare(Callback)
and wait for the callback. - Optional: Inspect the selected tracks using
getMappedTrackInfo(int)
andgetTrackSelections(int, int)
, and make adjustments usingclearTrackSelections(int)
,replaceTrackSelections(int, Parameters)
andaddTrackSelection(int, Parameters)
. - Create a download request for the selected track using
getDownloadRequest(byte[])
. - Release the helper using
release()
.
- Build the helper using one of the
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DownloadHelper.Callback
A callback to be notified when theDownloadHelper
is prepared.static class
DownloadHelper.LiveContentUnsupportedException
Thrown at an attempt to download live content.
-
Field Summary
Fields Modifier and Type Field Description static DefaultTrackSelector.Parameters
DEFAULT_TRACK_SELECTOR_PARAMETERS
Deprecated.This instance does not haveContext
constraints.static DefaultTrackSelector.Parameters
DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT
Default track selection parameters for downloading, but without anyContext
constraints.static DefaultTrackSelector.Parameters
DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_VIEWPORT
Deprecated.This instance does not haveContext
constraints.
-
Constructor Summary
Constructors Constructor Description DownloadHelper(MediaItem mediaItem, MediaSource mediaSource, DefaultTrackSelector.Parameters trackSelectorParameters, RendererCapabilities[] rendererCapabilities)
Creates download helper.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addAudioLanguagesToSelection(String... languages)
Convenience method to add selections of tracks for all specified audio languages.void
addTextLanguagesToSelection(boolean selectUndeterminedTextLanguage, String... languages)
Convenience method to add selections of tracks for all specified text languages.void
addTrackSelection(int periodIndex, DefaultTrackSelector.Parameters trackSelectorParameters)
Adds a selection of tracks to be downloaded.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.void
clearTrackSelections(int periodIndex)
Clears the selection of tracks for a period.static MediaSource
createMediaSource(DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory)
Equivalent tocreateMediaSource(downloadRequest, dataSourceFactory, null)
.static MediaSource
createMediaSource(DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory, DrmSessionManager drmSessionManager)
Utility method to create aMediaSource
that only exposes the tracks defined indownloadRequest
.static DownloadHelper
forDash(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.static DownloadHelper
forDash(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.static DownloadHelper
forHls(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.static DownloadHelper
forHls(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.static DownloadHelper
forMediaItem(Context context, MediaItem mediaItem)
Creates aDownloadHelper
for the given progressive media item.static DownloadHelper
forMediaItem(Context context, MediaItem mediaItem, RenderersFactory renderersFactory, DataSource.Factory dataSourceFactory)
Creates aDownloadHelper
for the given media item.static DownloadHelper
forMediaItem(MediaItem mediaItem, DefaultTrackSelector.Parameters trackSelectorParameters, RenderersFactory renderersFactory, DataSource.Factory dataSourceFactory)
Creates aDownloadHelper
for the given media item.static DownloadHelper
forMediaItem(MediaItem mediaItem, DefaultTrackSelector.Parameters trackSelectorParameters, RenderersFactory renderersFactory, DataSource.Factory dataSourceFactory, DrmSessionManager drmSessionManager)
Creates aDownloadHelper
for the given media item.static DownloadHelper
forProgressive(Context context, Uri uri)
Deprecated.static DownloadHelper
forProgressive(Context context, Uri uri, String cacheKey)
Deprecated.static DownloadHelper
forSmoothStreaming(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.static DownloadHelper
forSmoothStreaming(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.static DownloadHelper
forSmoothStreaming(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.static DefaultTrackSelector.Parameters
getDefaultTrackSelectorParameters(Context context)
Returns the default parameters used for track selection for downloading.DownloadRequest
getDownloadRequest(byte[] data)
Builds aDownloadRequest
for downloading the selected tracks.DownloadRequest
getDownloadRequest(String id, byte[] data)
Builds aDownloadRequest
for downloading the selected tracks.Object
getManifest()
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
getPeriodCount()
Returns the number of periods for which media is available.static RendererCapabilities[]
getRendererCapabilities(RenderersFactory renderersFactory)
Extracts renderer capabilities for the renderers created by the provided renderers factory.TrackGroupArray
getTrackGroups(int periodIndex)
Returns the track groups for the given period.List<ExoTrackSelection>
getTrackSelections(int periodIndex, int rendererIndex)
Returns alltrack selections
for a period and renderer.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(int periodIndex, DefaultTrackSelector.Parameters trackSelectorParameters)
Replaces a selection of tracks to be downloaded.
-
-
-
Field Detail
-
DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT
public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT
Default track selection parameters for downloading, but without anyContext
constraints.If possible, use
getDefaultTrackSelectorParameters(Context)
instead.
-
DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_VIEWPORT
@Deprecated public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_VIEWPORT
Deprecated.This instance does not haveContext
constraints. UsegetDefaultTrackSelectorParameters(Context)
instead.
-
DEFAULT_TRACK_SELECTOR_PARAMETERS
@Deprecated public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS
Deprecated.This instance does not haveContext
constraints. UsegetDefaultTrackSelectorParameters(Context)
instead.
-
-
Constructor Detail
-
DownloadHelper
public DownloadHelper(MediaItem mediaItem, @Nullable MediaSource mediaSource, DefaultTrackSelector.Parameters trackSelectorParameters, RendererCapabilities[] rendererCapabilities)
Creates download helper.- Parameters:
mediaItem
- The media item.mediaSource
- AMediaSource
for which tracks are selected, or null if no track selection needs to be made.trackSelectorParameters
-DefaultTrackSelector.Parameters
for selecting tracks for downloading.rendererCapabilities
- TheRendererCapabilities
of the renderers for which tracks are selected.
-
-
Method Detail
-
getDefaultTrackSelectorParameters
public static DefaultTrackSelector.Parameters getDefaultTrackSelectorParameters(Context context)
Returns the default parameters used for track selection for downloading.
-
getRendererCapabilities
public static RendererCapabilities[] getRendererCapabilities(RenderersFactory renderersFactory)
Extracts renderer capabilities for the renderers created by the provided renderers factory.- Parameters:
renderersFactory
- ARenderersFactory
.- Returns:
- The
RendererCapabilities
for each renderer created by therenderersFactory
.
-
forProgressive
@Deprecated public static DownloadHelper forProgressive(Context context, Uri uri)
Deprecated.
-
forProgressive
@Deprecated public static DownloadHelper forProgressive(Context context, Uri uri, @Nullable String cacheKey)
Deprecated.
-
forDash
@Deprecated public static DownloadHelper forDash(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.
-
forDash
@Deprecated public static DownloadHelper forDash(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, @Nullable DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.
-
forHls
@Deprecated public static DownloadHelper forHls(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.
-
forHls
@Deprecated public static DownloadHelper forHls(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, @Nullable DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.
-
forSmoothStreaming
@Deprecated public static DownloadHelper forSmoothStreaming(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.
-
forSmoothStreaming
@Deprecated public static DownloadHelper forSmoothStreaming(Context context, Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory)
Deprecated.
-
forSmoothStreaming
@Deprecated public static DownloadHelper forSmoothStreaming(Uri uri, DataSource.Factory dataSourceFactory, RenderersFactory renderersFactory, @Nullable DrmSessionManager drmSessionManager, DefaultTrackSelector.Parameters trackSelectorParameters)
Deprecated.
-
forMediaItem
public static DownloadHelper forMediaItem(Context context, MediaItem mediaItem)
Creates aDownloadHelper
for the given progressive media item.- Parameters:
context
- The context.mediaItem
- AMediaItem
.- Returns:
- A
DownloadHelper
for progressive streams. - Throws:
IllegalStateException
- If the media item is of type DASH, HLS or SmoothStreaming.
-
forMediaItem
public static DownloadHelper forMediaItem(Context context, MediaItem mediaItem, @Nullable RenderersFactory renderersFactory, @Nullable DataSource.Factory dataSourceFactory)
Creates aDownloadHelper
for the given media item.- Parameters:
context
- The context.mediaItem
- AMediaItem
.renderersFactory
- ARenderersFactory
creating the renderers for which tracks are selected.dataSourceFactory
- ADataSource.Factory
used to load the manifest for adaptive streams. This argument is required for adaptive streams and ignored for progressive streams.- Returns:
- A
DownloadHelper
. - Throws:
IllegalStateException
- If the corresponding module is missing for DASH, HLS or SmoothStreaming media items.IllegalArgumentException
- If thedataSourceFactory
is null for adaptive streams.
-
forMediaItem
public static DownloadHelper forMediaItem(MediaItem mediaItem, DefaultTrackSelector.Parameters trackSelectorParameters, @Nullable RenderersFactory renderersFactory, @Nullable DataSource.Factory dataSourceFactory)
Creates aDownloadHelper
for the given media item.- Parameters:
mediaItem
- AMediaItem
.renderersFactory
- ARenderersFactory
creating the renderers for which tracks are selected.trackSelectorParameters
-DefaultTrackSelector.Parameters
for selecting tracks for downloading.dataSourceFactory
- ADataSource.Factory
used to load the manifest for adaptive streams. This argument is required for adaptive streams and ignored for progressive streams.- Returns:
- A
DownloadHelper
. - Throws:
IllegalStateException
- If the corresponding module is missing for DASH, HLS or SmoothStreaming media items.IllegalArgumentException
- If thedataSourceFactory
is null for adaptive streams.
-
forMediaItem
public static DownloadHelper forMediaItem(MediaItem mediaItem, DefaultTrackSelector.Parameters trackSelectorParameters, @Nullable RenderersFactory renderersFactory, @Nullable DataSource.Factory dataSourceFactory, @Nullable DrmSessionManager drmSessionManager)
Creates aDownloadHelper
for the given media item.- Parameters:
mediaItem
- AMediaItem
.renderersFactory
- ARenderersFactory
creating the renderers for which tracks are selected.trackSelectorParameters
-DefaultTrackSelector.Parameters
for selecting tracks for downloading.dataSourceFactory
- ADataSource.Factory
used to load the manifest for adaptive streams. This argument is required for adaptive streams and ignored for progressive streams.drmSessionManager
- An optionalDrmSessionManager
. Used to help determine which tracks can be selected.- Returns:
- A
DownloadHelper
. - Throws:
IllegalStateException
- If the corresponding module is missing for DASH, HLS or SmoothStreaming media items.IllegalArgumentException
- If thedataSourceFactory
is null for adaptive streams.
-
createMediaSource
public static MediaSource createMediaSource(DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory)
Equivalent tocreateMediaSource(downloadRequest, dataSourceFactory, null)
.
-
createMediaSource
public static MediaSource createMediaSource(DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory, @Nullable DrmSessionManager drmSessionManager)
Utility method to create aMediaSource
that only exposes the tracks defined indownloadRequest
.- Parameters:
downloadRequest
- ADownloadRequest
.dataSourceFactory
- A factory forDataSource
s to read the media.drmSessionManager
- An optionalDrmSessionManager
to be passed to theMediaSource
.- Returns:
- A
MediaSource
that only exposes the tracks defined indownloadRequest
.
-
prepare
public void prepare(DownloadHelper.Callback callback)
Initializes the helper for starting a download.- Parameters:
callback
- A callback to be notified when preparation completes or fails.- Throws:
IllegalStateException
- If the download helper has already been prepared.
-
release
public void release()
Releases the helper and all resources it is holding.
-
getManifest
@Nullable public Object getManifest()
Returns the manifest, or null if no manifest is loaded. Must not be called until after preparation completes.
-
getPeriodCount
public int getPeriodCount()
Returns the number of periods for which media is available. Must not be called until after preparation completes.
-
getTrackGroups
public TrackGroupArray getTrackGroups(int periodIndex)
Returns the track groups for the given period. Must not be called until after preparation completes.Use
getMappedTrackInfo(int)
to get the track groups mapped to renderers.- Parameters:
periodIndex
- The period index.- Returns:
- The track groups for the period. May be
TrackGroupArray.EMPTY
for single stream content.
-
getMappedTrackInfo
public MappingTrackSelector.MappedTrackInfo getMappedTrackInfo(int periodIndex)
Returns the mapped track info for the given period. Must not be called until after preparation completes.- Parameters:
periodIndex
- The period index.- Returns:
- The
MappingTrackSelector.MappedTrackInfo
for the period.
-
getTrackSelections
public List<ExoTrackSelection> getTrackSelections(int periodIndex, int rendererIndex)
Returns alltrack selections
for a period and renderer. Must not be called until after preparation completes.- Parameters:
periodIndex
- The period index.rendererIndex
- The renderer index.- Returns:
- A list of selected
track selections
.
-
clearTrackSelections
public void clearTrackSelections(int periodIndex)
Clears the selection of tracks for a period. Must not be called until after preparation completes.- Parameters:
periodIndex
- The period index for which track selections are cleared.
-
replaceTrackSelections
public void replaceTrackSelections(int periodIndex, DefaultTrackSelector.Parameters trackSelectorParameters)
Replaces a selection of tracks to be downloaded. Must not be called until after preparation completes.- Parameters:
periodIndex
- The period index for which the track selection is replaced.trackSelectorParameters
- TheDefaultTrackSelector.Parameters
to obtain the new selection of tracks.
-
addTrackSelection
public void addTrackSelection(int periodIndex, DefaultTrackSelector.Parameters trackSelectorParameters)
Adds a selection of tracks to be downloaded. Must not be called until after preparation completes.- Parameters:
periodIndex
- The period index this track selection is added for.trackSelectorParameters
- TheDefaultTrackSelector.Parameters
to obtain the new selection of tracks.
-
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 after preparation completes.- Parameters:
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 after preparation completes.- Parameters:
selectUndeterminedTextLanguage
- Whether a text track with undetermined language should be selected for downloading if no track with one of the specifiedlanguages
is available.languages
- A list of text languages for which tracks should be added to the download selection, as IETF BCP 47 conformant tags.
-
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 after preparation completes.- Parameters:
periodIndex
- The period index the track selection is added for.rendererIndex
- The renderer index the track selection is added for.trackSelectorParameters
- TheDefaultTrackSelector.Parameters
to obtain the new selection of tracks.overrides
- A list ofSelectionOverrides
to apply to thetrackSelectorParameters
. If empty,trackSelectorParameters
are used as they are.
-
getDownloadRequest
public DownloadRequest getDownloadRequest(@Nullable byte[] data)
Builds aDownloadRequest
for downloading the selected tracks. Must not be called until after preparation completes. The uri of theDownloadRequest
will be used as content id.- Parameters:
data
- Application provided data to store inDownloadRequest.data
.- Returns:
- The built
DownloadRequest
.
-
getDownloadRequest
public DownloadRequest getDownloadRequest(String id, @Nullable byte[] data)
Builds aDownloadRequest
for downloading the selected tracks. Must not be called until after preparation completes.- Parameters:
id
- The unique content id.data
- Application provided data to store inDownloadRequest.data
.- Returns:
- The built
DownloadRequest
.
-
-