Downloader
**********
The Downloader plugin provides a way to download content for offline
playback. The plugin supports downloading DASH and HLS content with or
without DRM.
Setup
=====
First, install the "@castlabs/react-native-prestoplay-downloader"
package:
npm install @castlabs/react-native-prestoplay-downloader
Once the plugin is installed, enable it when initializing the SDK, by
passing the "DownloaderPlugin"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/classes/DownloaderPlugin/)
class. For example:
import { Sdk } from "@castlabs/react-native-prestoplay";
import { DownloaderPlugin } from "@castlabs/react-native-prestoplay-downloader";
Sdk.initialize({
licenseKey: {
/* Valid license here */
},
plugins: [DownloaderPlugin],
});
Android
-------
The Downloader requires the Android "FOREGROUND_SERVICE" permission to
function. Optionally, the "POST_NOTIFICATIONS" permission may be
included to enable download progress notifications. Permissions should
be added to the "./android/app/src/main/AndroidManifest.xml" file, for
example:
Usage
=====
Create download
---------------
A new download is initiated using "DownloadService.createDownload"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/classes/DownloadService/#createdownload),
which requires a unique download ID and download configuration. The
asset ID may be used as the download ID as long as it guarantees
uniqueness. Since creating a download involves fetching the manifest
and preparing the download, the process may take some time. When a
download instance is no longer needed for the given scope, call
"Download.destroy"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#destroy)
to unsubscribe from native events. For example:
import { ContentType } from "@castlabs/react-native-prestoplay";
import { DownloadService } from "@castlabs/react-native-prestoplay-downloader";
const download = await DownloadService.getInstance().createDownload(
"",
{
autoPlay: true,
source: {
url: "https://demo.castlabs.com/media/TOS/abr/Manifest_clean_sizes.mpd",
type: ContentType.Dash,
drmProtected: false,
},
},
);
// Destroy a download instance when it is no longer needed.
// For example when the component is unmounted.
// This is important to avoid memory leaks.
download.destroy();
Get download
------------
The download can always be retrieved with
"DownloadService.getDownload"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/classes/DownloadService/#getdownload).
It returns "null" if the download does not exist. For example:
import { DownloadService } from "@castlabs/react-native-prestoplay-downloader";
const download = await DownloadService.getInstance().getDownload(
"",
);
if (download) {
// Download exists
} else {
// Download does not exist
}
Start download
--------------
Once the download is ready, it is started using "Download.start"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#start)
with selected tracks. A list of available tracks can be retrieved
using "Download.getDownloadableTracks"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#getdownloadabletracks).
Once initiated, the download progress and state changes are tracked
using the "downloadStateChanged"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/EventType/)
and "downloadProgressChanged"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/EventType/)
events. Use "Download.addEventListener"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#addeventlistener)
to attach listeners to these events. Alternatively, React components
can use the "useDownloadState"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/functions/useDownloadState/)
or "useDownloadProgress"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/functions/useDownloadProgress/)
hooks. The download can be paused with "Download.pause"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#pause)
and resumed later with "Download.resume"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/interfaces/Download/#resume).
const downloadableTracks = download.getDownloadableTracks();
await download.start({
videoRendition: downloadableTracks.videoRenditions[0],
audioTracks: [downloadableTracks.audioTracks[0]],
textTracks: [],
});
Play downloaded content
-----------------------
The downloaded content can be played back using the same player
configuration originally used to download it - only the source URL is
changed to point to the downloaded manifest. For example:
playerConfiguration.source.url = download.getLocalManifestUri();
Delete download
---------------
When the download is no longer needed, remove it using
"DownloadService.deleteDownload"
(https://players.castlabs.com/react-native/1.4.2/docs/downloader-plugin-api/classes/DownloadService/#deletedownload).
This method erases all downloaded content and its associated metadata
from local storage.
import { DownloadService } from "@castlabs/react-native-prestoplay-downloader";
await DownloadService.getInstance().deleteDownload("");