AndroidMediaPlayer.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //
  5. package ai.nreal.videoplayer;
  6. import android.content.Context;
  7. import android.content.res.AssetFileDescriptor;
  8. import android.media.MediaPlayer;
  9. import android.media.MediaPlayer.OnCompletionListener;
  10. import android.media.MediaPlayer.OnErrorListener;
  11. import android.media.MediaPlayer.OnPreparedListener;
  12. import android.net.Uri;
  13. import android.util.Log;
  14. import android.view.Surface;
  15. import java.io.IOException;
  16. public class AndroidMediaPlayer implements IVideoPlayer {
  17. private static final String TAG = "AndroidMediaPlayer";
  18. private MediaPlayer mMediaPlayer = new MediaPlayer();
  19. private IVideoPlayerEventProxy mVideoPlayerEvent;
  20. private boolean mIsReady = false;
  21. private Context mContext;
  22. private Surface mSurface;
  23. public AndroidMediaPlayer() {
  24. }
  25. public void init(Context context, IVideoPlayerEventProxy event) {
  26. this.mVideoPlayerEvent = event;
  27. this.mContext = context;
  28. }
  29. public void setSurface(Surface surface) {
  30. this.mSurface = surface;
  31. this.mMediaPlayer.setSurface(this.mSurface);
  32. }
  33. public void load(String path, boolean isDrm) {
  34. if (isDrm)
  35. {
  36. Log.i("AndroidMediaPlayer", "not support drm content, you can develop your own drm player here : " + path);
  37. return;
  38. }
  39. try {
  40. Log.i("AndroidMediaPlayer", "load: " + path);
  41. this.mMediaPlayer.setDataSource(this.mContext, Uri.parse(path));
  42. this.mMediaPlayer.prepareAsync();
  43. } catch (IOException var3) {
  44. var3.printStackTrace();
  45. }
  46. final IVideoPlayerEventProxy event = this.mVideoPlayerEvent;
  47. this.mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
  48. public void onPrepared(MediaPlayer mp) {
  49. AndroidMediaPlayer.this.mIsReady = true;
  50. event.OnEvent(10001);
  51. Log.i("AndroidMediaPlayer", "onPrepared: ");
  52. mp.start();
  53. }
  54. });
  55. this.mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
  56. public void onCompletion(MediaPlayer mediaPlayer) {
  57. event.OnEvent(10003);
  58. Log.i("AndroidMediaPlayer", "onCompletion: ");
  59. }
  60. });
  61. this.mMediaPlayer.setOnErrorListener(new OnErrorListener() {
  62. public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
  63. event.OnEvent(10004);
  64. Log.i("AndroidMediaPlayer", "onError: ");
  65. return false;
  66. }
  67. });
  68. }
  69. public void loadAsset(AssetFileDescriptor file) {
  70. try {
  71. this.mMediaPlayer.setDataSource(file);
  72. this.mMediaPlayer.prepareAsync();
  73. } catch (IOException var3) {
  74. var3.printStackTrace();
  75. }
  76. final IVideoPlayerEventProxy event = this.mVideoPlayerEvent;
  77. this.mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
  78. public void onPrepared(MediaPlayer mp) {
  79. AndroidMediaPlayer.this.mIsReady = true;
  80. event.OnEvent(10001);
  81. Log.i("AndroidMediaPlayer", "onPrepared: ");
  82. mp.start();
  83. }
  84. });
  85. this.mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
  86. public void onCompletion(MediaPlayer mediaPlayer) {
  87. event.OnEvent(10003);
  88. Log.i("AndroidMediaPlayer", "onCompletion: ");
  89. }
  90. });
  91. this.mMediaPlayer.setOnErrorListener(new OnErrorListener() {
  92. public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
  93. event.OnEvent(10004);
  94. Log.i("AndroidMediaPlayer", "onError: ");
  95. return false;
  96. }
  97. });
  98. }
  99. public void play() {
  100. if (!this.mMediaPlayer.isPlaying() && this.mIsReady) {
  101. this.mMediaPlayer.start();
  102. }
  103. }
  104. public void pause() {
  105. if (this.mIsReady) {
  106. this.mMediaPlayer.pause();
  107. }
  108. }
  109. public void release() {
  110. if (this.mIsReady) {
  111. this.mMediaPlayer.stop();
  112. this.mMediaPlayer.release();
  113. }
  114. }
  115. }