Skip to content
On this page

ExoPlayer phiên bản 2.9.x ​

1. Yêu cầu ​

Những yêu cầu chung xem ở đâ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.9.0'
  // If you don't use feature license encrypt, please comment line below
  implementation 'com.sigma.packer:2.9.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);
        // FIXME: If you use license encrypt feature please uncomment line below
        // drmSessionManager = new DefaultDrmSessionManager<>(drmSchemeUuid, SigmaMediaDrm.newInstance(drmSchemeUuid), drmCallback, null, true);
        drmSessionManager = new DefaultDrmSessionManager<>(drmSchemeUuid, FrameworkMediaDrm .newInstance(drmSchemeUuid), drmCallback, null, true);
      }
      catch (Exception ex) {
        drmSessionManager = null;
      }
    } else {
      drmSessionManager = null;
    }
    return drmSessionManager;
}

3.3. Tích hợp DrmSessionManager vào MediaSource ​

java
private MediaSource createMediaSource(String mediaUrl) {
    MediaSource mediaSource;
    Uri uri = Uri.parse(mediaUrl);
    @C.ContentType int type = Util.inferContentType(uri, "");
    if (type == C.TYPE_DASH) {
        mediaSource =
            new DashMediaSource.Factory(dataSourceFactory)
                .createMediaSource(uri);
    } else if (type == C.TYPE_OTHER) {
        mediaSource =
          new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
    } else {
        throw new IllegalStateException();
    }
    return mediaSource;
}

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);
}
private synchronized Cache getDownloadCache() {
  if (downloadCache == null) {
    File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY);
    downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor());
  }
  return downloadCache;
}
private static CacheDataSourceFactory buildReadOnlyCacheDataSource(
  DefaultDataSourceFactory upstreamFactory, Cache cache) {
  return new CacheDataSourceFactory(
    cache,
    upstreamFactory,
    new FileDataSourceFactory(),
    /* cacheWriteDataSinkFactory= */ null,
    CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
    /* eventListener= */ null);
}

private File getDownloadDirectory() {
  if (downloadDirectory == null) {
    downloadDirectory = getExternalFilesDir(null);
    if (downloadDirectory == null) {
      downloadDirectory = getFilesDir();
    }
  }
  return downloadDirectory;
}

3.5. Khởi tạo Player với MediaSource ​

java
protected boolean initializePlayer() {
  DrmSessionManager drmSessionManager = createDrmSessionManager();

  DataSource.Factory dataSourceFactory = buildDataSourceFactory();
  MediaSource mediaSource = createMediaSource(MEDIA_URL);
  trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory());
   DefaultTrackSelector.Parameters trackSelectionParameters = new DefaultTrackSelector.ParametersBuilder().build();
  player = ExoPlayerFactory.newSimpleInstance(
      /* context= */ this, buildRenderersFactory(true), trackSelector, drmSessionManager);
  playerView.setPlayer(player);
  player.setPlayWhenReady(true);
  player.prepare(mediaSource, true, false);
  return true;
}

4. Mã nguồn mẫu ​

Mã nguồn mẫu