This project helps to play the Widevine DRM-protected video in Android ExoPlayer. Check Quick Guide: Integrating Widevine DRM In ExoPlayer For Android blog for more details.
To play the DRM-protected video in ExoPlayer, you will require the below URLs from the backend setup:
- Manifest URL (Media file URL)
- Widevine license URL
- JWT token (A way of authorizing users who make a decryption key request)
Update WIDEVINE_URL
, URL
& jwtToken
fields in PlayerActivity
with required data.
-
Add ExoPlayer dependency in
build.gradle
:implementation 'com.google.android.exoplayer:exoplayer:2.10.1'
-
Add PlayerView in the layout:
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/player_view" android:layout_width="match_parent" android:layout_height="match_parent" />
-
ExoPlayer supports different streaming methods, including MPEG-DASH, HLS, SmoothStreaming. As I'm performing DASH implementation in the project, hence I need to use
DashMediaSource
class and pass themanifestURL
.MediaSourceFactory mediaSrcFactory = new DashMediaSource.Factory(this::newDefaultHttpDataSource); MediaSource targetMediaSource = mediaSrcFactory.createMediaSource(manifestURL);
-
Before creating a media source we need to instantiate
HttpMediaDrmCallback
by adding thewidevineURL
in its constructor.HttpMediaDrmCallback
helps to perform the Widevine license exchange.HttpMediaDrmCallback mediaDrmCallback = new HttpMediaDrmCallback(widevineURL, new DefaultHttpDataSourceFactory(userAgentString)); DrmSessionManager drmSessionManager = new DefaultDrmSessionManager.Builder().build(mediaDrmCallback); mediaSrcFactory.setDrmSessionManager(drmSessionManager);
-
The license server needs an authentication token before issuing the license. Hence while requesting a DRM license, we need to append the token authorization header in the request. Here I will be sending the
jwtToken
in the request with the help ofHttpMediaDrmCallback
instance.mediaDrmCallback.setKeyRequestProperty("Authorization", "Bearer="+jwtToken);
That's it! Run the code and watch the DRM-protected video in the native ExoPlayer.