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

  • Media ID
  • Policy ID
  • If your app uses offline downloads and targets API level 34 or later, the app must declare the required foreground-service permissions in AndroidManifest.xml.


Implementation

  1. Update the AndroidManifest.xml file.
  2. 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.
  3. Use the Android offline DRM recipe to prepare and play the downloaded content.



Update the AndroidManifest.xml

  1. 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" />
  2. 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>


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 MediaDrmCallback returned 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.


Did this page help you?
© 2007- Longtail Ad Solutions, Inc.