ExoMediaPlayer.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package ai.nreal.videoplayer;
  2. import android.content.Context;
  3. import android.net.Uri;
  4. import android.util.Log;
  5. import android.view.Surface;
  6. import android.widget.Toast;
  7. import com.google.android.exoplayer2.C;
  8. import com.google.android.exoplayer2.MediaItem;
  9. import com.google.android.exoplayer2.SimpleExoPlayer;
  10. import com.google.android.exoplayer2.MediaItem.DrmConfiguration;
  11. import com.google.android.exoplayer2.MediaItem.PlaybackProperties;
  12. import com.google.android.exoplayer2.SimpleExoPlayer.Builder;
  13. import com.google.android.exoplayer2.drm.FrameworkMediaDrm;
  14. import com.google.android.exoplayer2.util.Assertions;
  15. import com.google.android.exoplayer2.util.Util;
  16. public class ExoMediaPlayer implements IVideoPlayer {
  17. private static final String TAG = "ExoMediaPlayer";
  18. public SimpleExoPlayer player;
  19. private IVideoPlayerEventProxy eventProxy;
  20. private Context mContext;
  21. private static final String ACTION_VIEW = "com.google.android.exoplayer.surfacedemo.action.VIEW";
  22. private static final String EXTENSION_EXTRA = "extension";
  23. private static final String DRM_SCHEME_EXTRA = "widevine";
  24. public static final String MIME_TYPE_DASH = "application/dash+xml";
  25. public static final String MIME_TYPE_HLS = "application/x-mpegURL";
  26. public static final String MIME_TYPE_SS = "application/vnd.ms-sstr+xml";
  27. public static final String MIME_TYPE_VIDEO_MP4 = "video/mp4";
  28. public ExoMediaPlayer() {
  29. Log.i("ExoMediaPlayer", "ExoMediaPlayer: ");
  30. }
  31. public void init(Context context, IVideoPlayerEventProxy event) {
  32. Log.i("ExoMediaPlayer", "init: ");
  33. this.player = (new Builder(context)).build();
  34. this.eventProxy = event;
  35. this.mContext = context;
  36. }
  37. public void setSurface(Surface mSurface) {
  38. this.player.setVideoSurface(mSurface);
  39. }
  40. public void load(String path, boolean isDrm) {
  41. Log.i("ExoMediaPlayer", "load: " + path);
  42. MediaItem item = null;
  43. if (isDrm) {
  44. item = this.getDRMItem(path);
  45. } else {
  46. item = this.getNormalItem(path);
  47. }
  48. this.player.setMediaItem(item);
  49. this.player.prepare();
  50. this.player.play();
  51. this.player.setRepeatMode(2);
  52. this.eventProxy.OnEvent(10001);
  53. }
  54. private MediaItem getDRMItem(String path) {
  55. MediaItem item = (new com.google.android.exoplayer2.MediaItem.Builder()).setUri(Uri.parse(path)).setMediaMetadata((new com.google.android.exoplayer2.MediaMetadata.Builder()).setTitle("Widevine DASH cenc: Tears").build()).setMimeType("application/dash+xml").setDrmUuid(C.WIDEVINE_UUID).setDrmLicenseUri("https://proxy.uat.widevine.com/proxy?provider=widevine_test").build();
  56. this.checkMediaItem(item);
  57. return item;
  58. }
  59. private MediaItem getNormalItem(String path) {
  60. return (new com.google.android.exoplayer2.MediaItem.Builder()).setUri(Uri.parse(path)).build();
  61. }
  62. private void checkMediaItem(MediaItem mediaItem) {
  63. if (!Util.checkCleartextTrafficPermitted(new MediaItem[]{mediaItem})) {
  64. Log.i("ExoMediaPlayer", "checkMediaItem: ");
  65. }
  66. DrmConfiguration drmConfiguration = ((PlaybackProperties)Assertions.checkNotNull(mediaItem.playbackProperties)).drmConfiguration;
  67. if (drmConfiguration != null) {
  68. if (Util.SDK_INT < 18) {
  69. Log.i("ExoMediaPlayer", "checkMediaItem: error_drm_unsupported_before_api_18");
  70. } else if (!FrameworkMediaDrm.isCryptoSchemeSupported(drmConfiguration.uuid)) {
  71. Log.i("ExoMediaPlayer", "checkMediaItem: error_drm_unsupported_scheme");
  72. }
  73. }
  74. }
  75. private void showToast(int messageId) {
  76. this.showToast(this.mContext.getString(messageId));
  77. }
  78. private void showToast(String message) {
  79. Toast.makeText(this.mContext, message, 1).show();
  80. }
  81. public void play() {
  82. Log.i("ExoMediaPlayer", "play: ");
  83. this.player.play();
  84. }
  85. public void pause() {
  86. Log.i("ExoMediaPlayer", "pause: ");
  87. this.player.pause();
  88. }
  89. public void release() {
  90. Log.i("ExoMediaPlayer", "release: ");
  91. this.player.release();
  92. }
  93. }