Configure offline DRM (Android)
Allow users to download and view DRM content without connection to the Internet.
By requiring authorization from a key manager, DRM protects content from unauthorized consumption. However, there are situations when your viewers are offline and cannot immediately request this authorization. This article explains how to set up offline DRM playback in your Android app.
Prerequisites
| Item | Descrption |
|---|---|
| Media ID | Identifier for the media |
| Policy ID | Identifier for the JWX DRM policy |
| Foreground-service permissions | If your app uses offline downloads and targets API level 34 or later, declare the The SDK declares the download service itself, with the dataSync foreground-service type. |
Implementation
-
Update the AndroidManifest.xml file.
-
Add the following permissions required for offline downloads.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -
If your app targets API level 34 or later, also add the following permission.
The SDK declares the services required for offline downloads. Do not declare the download or platform scheduler services in your app. If your app already declares these services, remove the declarations when upgrading.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> <application> ... </application> </manifest> -
- Make a
GET https://cdn.jwplayer.com/v2/media/{media_id}/drm/{policy_id}request to retrieve the offline DRM content. Replace{media_id}and{policy_id}with your values.
-
Use the Android offline DRM recipe to prepare and play the downloaded content.
Offline download errorsUse
OfflineDownloadManager.getDownloads(context)to retrieve download information. For failed downloads,OfflineDownloadInfo.getFailureReason()provides the associated exception.During the current application process, the original exception is available. After the application restarts, only a failure code is retained, so
getFailureReason()returns a generic exception indicating that the download failed without identifying the cause. To preserve detailed diagnostics, capture the exception inMediaDownloadResultListener.onMediaDownloadFailed(String mediaId, Exception exception)when the failure occurs.For more information, see OfflineDownloadInfo.
Use a custom DRM callback
A MediaDrmCallback configured on a PlaylistItem is used for both streaming and offline license acquisition. The callback remains available while the OfflineDownloadManager that prepared the item is alive.
If your license server requires custom authorization, register an OfflineDrmCallbackProvider from Application.onCreate(). The SDK uses the provider when the item-level callback is unavailable, including after the app starts a new process.
The
MediaDrmCallbackreturned by the provider must know the license server URL. Do not rely on the request to provide the URL because it is empty when the SDK releases an offline license after a download is removed.
@Override
public void onCreate() {
super.onCreate();
OfflineDownloadFactory.setOfflineDrmCallbackProvider(
mediaId -> new MyDrmCallback(getApplicationContext(), mediaId)
);
}Register the provider on every app launch. Do not capture an Activity in the provider or returned callbacks. The provider should return quickly. Perform license requests in the returned MediaDrmCallback, which runs on a background thread.
Manage offline licenses
When removeDownload() completes, the SDK attempts to release the associated offline Widevine license. Re-downloading the same media also attempts to release the superseded license on a best-effort basis.
Use getOfflineLicenseDurationRemaining() to check when a license is approaching expiration. Use refreshOfflineLicense() to renew a renewable license without downloading the media again. Refresh requires a network connection and a license server that supports renewal, and must occur before the license expires.
After a successful refresh, retrieve a new PlaylistItem with getDownloadedPlaylistItem() before playback because previously retrieved items contain the superseded key set. If renewal fails, the existing download and license remain unchanged.
For method signatures and callback details, see OfflineDownloadManager in the Android SDK reference.
Updated 3 days ago
