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
class. For example:
import {Sdk} from '@castlabs/react-native-prestoplay';
import {IOSDashPlugin} from "@castlabs/react-native-prestoplay-ios-dash";
Sdk.initialize({
licenseKey: { /* Valid license here */},
plugins: [DownloaderPlugin],
});
Usage
Create download
A new download is initiated using DownloadService.createDownload, which requires a unique download ID and player configuration. The asset ID may be used as the download ID as long as it guarantees uniqueness. The same player configuration is used for playback later. 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 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(
"<UNIQUE_DOWNLOAD_ID>",
{
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; 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("<UNIQUE_DOWNLOAD_ID>",);
if (download) {
// Download exists
} else {
// Download does not exist
}
Start download
Once the download is ready, it is started using Download.start with the previously specified download ID (which must match the one provided during creation) and a selected track. A list of available tracks can be retrieved using Download.getDownloadableTracks. Once initiated, the download progress and state changes are tracked using the downloadStateChanged and downloadProgressChanged events. Use Download.addEventListener to attach listeners to these events. Alternatively, React components can use the useDownloadState or useDownloadProgress hooks. The download can be paused with Download.pause and resumed later with 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. 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("<UNIQUE_DOWNLOAD_ID>",);