AndroidVideoPlayer.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using UnityEngine;
  3. namespace NRKernal.Experimental.NRExamples
  4. {
  5. public class AndroidVideoPlayer
  6. {
  7. public class AndroidVideoPlayerEventProxy : AndroidJavaProxy
  8. {
  9. public const int ONPREPARED = 10001;
  10. public const int ONFRAMEREADY = 10002;
  11. public const int ONCOMPLETED = 10003;
  12. public const int ONERROR = 10004;
  13. public AndroidVideoPlayerEventProxy() : base("ai.nreal.videoplayer.IVideoPlayerEventProxy")
  14. {
  15. }
  16. public void OnEvent(int eventid)
  17. {
  18. Debug.Log("AndroidVideoPlayerEventProxy:" + eventid);
  19. }
  20. }
  21. AndroidJavaObject m_MediaPlayerObject;
  22. AndroidJavaObject MediaPlayerObject
  23. {
  24. get
  25. {
  26. if (m_MediaPlayerObject == null)
  27. {
  28. //m_MediaPlayerObject = new AndroidJavaObject("ai.nreal.videoplayer.AndroidMediaPlayer");
  29. m_MediaPlayerObject = new AndroidJavaObject("ai.nreal.videoplayer.ExoMediaPlayer");
  30. }
  31. return m_MediaPlayerObject;
  32. }
  33. }
  34. AndroidJavaObject m_UnityActivity;
  35. AndroidJavaObject UnityActivity
  36. {
  37. get
  38. {
  39. if (m_UnityActivity == null)
  40. {
  41. AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  42. m_UnityActivity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  43. }
  44. return m_UnityActivity;
  45. }
  46. }
  47. public void InitWithMediaPlayer(IntPtr surface, string url, bool usedrm)
  48. {
  49. MediaPlayerObject.Call("init", UnityActivity, new AndroidVideoPlayerEventProxy());
  50. var methodID = AndroidJNI.GetMethodID(MediaPlayerObject.GetRawClass(), "setSurface", "(Landroid/view/Surface;)V");
  51. jvalue[] jniArgs = new jvalue[1];
  52. jniArgs[0].l = surface;
  53. AndroidJNI.CallVoidMethod(MediaPlayerObject.GetRawObject(), methodID, jniArgs);
  54. MediaPlayerObject.Call("load", url, usedrm);
  55. }
  56. public void Play()
  57. {
  58. Debug.Log("AndroidVideoPlayerEventProxy Play");
  59. MediaPlayerObject.Call("play");
  60. }
  61. public void Pause()
  62. {
  63. Debug.Log("AndroidVideoPlayerEventProxy Pause");
  64. MediaPlayerObject.Call("pause");
  65. }
  66. public void Release()
  67. {
  68. Debug.Log("AndroidVideoPlayerEventProxy Release");
  69. MediaPlayerObject.Call("release");
  70. }
  71. }
  72. }