How to use
Initialize and Authorize
To use Stream Downloader plugin, you need to initialize and authorize it. You can do it in any place in your code.
You need to initialize and authorize Stream Downloader plugin before using it. Otherwise, you will get an error.
import { registerPlugin, isRegistered } from "@twg/react-native-video-stream-downloader";
const initializePlugin = async () => {
const success = await registerPlugin("YOUR_API_KEY");
if (!success) {
throw new Error("Stream Downloader plugin is not authorized");
}
};
initializePlugin();
If you want to check if plugin is already authorized, you can use isRegistered
function.
import { isRegistered } from "@twg/react-native-video-stream-downloader";
const isAuthorized = isRegistered();
Download Stream or Video
After you initialize and authorize plugin, you can download stream or video.
import { downloadStream } from "@twg/react-native-video-stream-downloader";
const downloadStatus = await downloadStream("https://example.com/stream.m3u8", config);
downloadStream
function has two parameters:
url
- url of the stream or videoconfig
- configuration object (see Configuration)
see DownloadStatus
type in API Reference
downloadStream
function returns downloadStatus
object. You need to save it to play downloaded stream/video later. function return value once download is added to the queue not when download is finished.
Updating Download Progress
Download Status
You can get download progress in two ways:
- Using
onDownloadProgress
event
import { useEvent } from "@twg/react-native-video-stream-downloader";
useEvent("onDownloadProgress", (downloadsStatus: Array<DownloadStatus>) => {
console.log(downloadsStatus);
});
- using
getDownloadsStatus
function
import { getDownloadsStatus } from "@twg/react-native-video-stream-downloader";
const downloadsStatus: Array<DownloadStatus> = getDownloadsStatus();
- using
getDownloadStatus
function
import { getDownloadStatus } from "@twg/react-native-video-stream-downloader";
const downloadStatus: DownloadStatus = getDownloadStatus(downloadId);
Download End Event
to listen for download end event, you can use onDownloadEnd
event.
import { useEvent } from "@twg/react-native-video-stream-downloader";
useEvent("onDownloadEnd", (downloadStatus: DownloadStatus) => {
console.log(downloadStatus);
});
Pause, Resume, Cancel Download
You can pause, resume or cancel download using pauseDownload
, resumeDownload
and cancelDownload
functions.
import { pauseDownload, resumeDownload, cancelDownload } from "@twg/react-native-video-stream-downloader";
const download = await downloadStream("https://example.com/stream.m3u8", config);
const downloadId = download.id;
// pause download
pauseDownload(downloadId);
// resume download
resumeDownload(downloadId);
// cancel download
cancelDownload(downloadId);
Playing Downloaded Stream
Stream Downloader plugin will automatically play downloaded stream/video if there is downloaded content with the same url. So you don't need to do anything else
Manage Downloaded Assets
Get All Downloaded Assets
import { getDownloadedAssets } from "@twg/react-native-video-stream-downloader";
const downloadedAssets: Array<DownloadedAsset> = getDownloadedAssets();
see DownloadedAsset
type in API Reference
Delete All Downloaded Assets
import { deleteAllDownloadedAssets } from "@twg/react-native-video-stream-downloader";
deleteAllDownloadedAssets();
Delete Downloaded Asset
import { deleteDownloadedAsset } from "@twg/react-native-video-stream-downloader";
const downloadedAssets: Array<DownloadedAsset> = getDownloadedAssets();
deleteDownloadedAsset(downloadedAssets[0].id);
download.id
from downloadStream
function is not the same as downloadedAsset.id
from getDownloadedAssets
function.