ExoPlayer từ phiên bản 2.12.x đến 2.15.x
1. Yêu cầu
Những yêu cầu chung ở đây
Yêu cầu khác
- Thêm địa chi kho chứa thư viện vào tệp build.gradle trong thư mục app.
java
implementation 'com.google.android.exoplayer:exoplayer:2.12.0'
// If you don't use feature license encrypt, please comment line below
implementation 'com.sigma.packer:2.12.x:1.0.3'
2. Tích hợp MediaDrmCallback
Vui lòng xem tại MediaCallback
3. Khởi tạo PlayerActivity sử dụng WidevineMediaDrmCallback
Nếu bạn đã có kinh nghiệm sử dụng Exoplayer, vui lòng bỏ qua phần này.
3.1. Khởi tạo MediaDrmCallback
java
private WidevineMediaDrmCallback createMediaDrmCallback(
String licenseUrl, String[] keyRequestPropertiesArray) {
HttpDataSource.Factory licenseDataSourceFactory =
buildHttpDataSourceFactory();
WidevineMediaDrmCallback drmCallback =
new WidevineMediaDrmCallback(licenseUrl, licenseDataSourceFactory);
if (keyRequestPropertiesArray != null) {
for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i],
keyRequestPropertiesArray[i + 1]);
}
}
return drmCallback;
}
3.2. Khởi tạo DrmSessionManager
java
private DrmSessionManager createDrmSessionManager() {
DrmSessionManager drmSessionManager;
if (Util.SDK_INT >= 18) {
try {
UUID drmSchemeUuid = Assertions.checkNotNull(Util.getDrmUuid("widevine"));
MediaDrmCallback drmCallback = createMediaDrmCallback(LICENSE_URL, null);
drmSessionManager =
new DefaultDrmSessionManager.Builder()
// FIXME: If you use license encrypt feature please uncomment line below
// .setUuidAndExoMediaDrmProvider(drmSchemeUuid, SigmaMediaDrm.DEFAULT_PROVIDER)
.setUuidAndExoMediaDrmProvider(drmSchemeUuid, FrameworkMediaDrm.DEFAULT_PROVIDER)
.setMultiSession(true)
.build(drmCallback);
}
catch (Exception ex) {
drmSessionManager = null;
}
} else {
drmSessionManager = null;
}
return drmSessionManager;
}
3.3. Tích hợp DrmSessionManager vào MediaSource
java
private static List<MediaItem> createMediaItems(String mediaUrl) {
List<MediaItem> mediaItems = new ArrayList<>();
mediaItems.add(createMediaItem(mediaUrl));
return mediaItems;
}
private static MediaItem createMediaItem(String mediaUrl) {
MediaItem.Builder mediaItem = new MediaItem.Builder();
mediaItem.setUri(Uri.parse(mediaUrl));
mediaItem.setDrmUuid(Assertions.checkNotNull(Util.getDrmUuid("widevine")));
mediaItem.setDrmLicenseUri(MediaInfo.getInstance().licenseUrl());
mediaItem.setDrmMultiSession(true);
return mediaItem.build();
}
3.4. Tạo ra các Factory
java
public HttpDataSource.Factory buildHttpDataSourceFactory() {
return new DefaultHttpDataSourceFactory(userAgent);
}
private DataSource.Factory buildDataSourceFactory() {
DefaultDataSourceFactory upstreamFactory =
new DefaultDataSourceFactory(this, buildHttpDataSourceFactory());
return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
}
private RenderersFactory buildRenderersFactory(boolean preferExtensionDecoders) {
int extensionRendererMode =
useExtensionRenderers()
? (preferExtensionRenderer
? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;
return new DefaultRenderersFactory(/* context= */ this)
.setExtensionRendererMode(extensionRendererMode);
}
3.5. Khởi tạo Player với MediaSource
java
protected boolean initializePlayer() {
DrmSessionManager drmSessionManager = createDrmSessionManager();
DataSource.Factory dataSourceFactory = buildDataSourceFactory();
trackSelector = new DefaultTrackSelector(this, new AdaptiveTrackSelection.Factory());
DefaultTrackSelector.Parameters trackSelectionParameters =
new DefaultTrackSelector.ParametersBuilder(this)
.setAllowVideoMixedMimeTypeAdaptiveness(true)
.setAllowVideoNonSeamlessAdaptiveness(true)
.build();
MediaSourceFactory mediaSourceFactory =
new DefaultMediaSourceFactory(dataSourceFactory)
.setDrmSessionManager(drmSessionManager)
.setAdViewProvider(playerView);
player =
new SimpleExoPlayer.Builder(/* context= */ this, buildRenderersFactory.buildRenderersFactory(true))
.setMediaSourceFactory(mediaSourceFactory)
.setTrackSelector(trackSelector)
.build();
player.setPlayWhenReady(true);
playerView.setPlayer(player);
player.setMediaItems(createMediaItems(MEDIA_URL));
player.prepare();
return true;
}