OverlayVideoPlayerExample.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using UnityEngine;
  3. namespace NRKernal.Experimental.NRExamples
  4. {
  5. public class OverlayVideoPlayerExample : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private NROverlay videoOverlayPrefab;
  9. private AndroidVideoPlayer m_VideoPlayer;
  10. private NROverlay current;
  11. public bool useDRMSource = false;
  12. public string videoUri_drm = "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears.mpd";
  13. public string videoUri_normal = "https://storage.googleapis.com/exoplayer-test-media-1/mp4/android-screens-10s.mp4";
  14. void Start()
  15. {
  16. if (useDRMSource)
  17. {
  18. LoadDrmItem();
  19. }
  20. else
  21. {
  22. LoadNormalItem();
  23. }
  24. }
  25. private void InitVideoSurface()
  26. {
  27. NRDebugger.Info("[OverlayVideoPlayerExample] OnLayerCreated.");
  28. var surfaceJo = current.GetSurfaceId();
  29. if (surfaceJo == IntPtr.Zero)
  30. {
  31. NRDebugger.Error("[OverlayVideoPlayerExample] InitVideoSurface faild...");
  32. return;
  33. }
  34. if (m_VideoPlayer == null)
  35. {
  36. m_VideoPlayer = new AndroidVideoPlayer();
  37. // Playing video from internet needs the permission of "android.permission.INTERNET",
  38. // Add it to your "AndroidManifest.xml" file in "Assets/Plugin".
  39. string url = useDRMSource ? videoUri_drm : videoUri_normal;
  40. m_VideoPlayer.InitWithMediaPlayer(surfaceJo, url, useDRMSource);
  41. }
  42. }
  43. public void LoadDrmItem() => LoadVideoItem(true);
  44. public void LoadNormalItem() => LoadVideoItem(false);
  45. private void LoadVideoItem(bool usedrm)
  46. {
  47. if (current != null)
  48. {
  49. m_VideoPlayer?.Release();
  50. m_VideoPlayer = null;
  51. GameObject.Destroy(current.gameObject);
  52. NRDebugger.Info("[OverlayVideoPlayerExample] OnLayerDestroied.");
  53. current.Destroy();
  54. }
  55. current = GameObject.Instantiate(videoOverlayPrefab.gameObject, transform).GetComponent<NROverlay>();
  56. current.gameObject.name = "Overlay-" + (usedrm ? "drm" : "normal");
  57. current.isProtectedContent = usedrm;
  58. current.externalSurfaceObjectCreated += InitVideoSurface;
  59. current.gameObject.SetActive(true);
  60. useDRMSource = usedrm;
  61. }
  62. private void OnApplicationPause(bool pause)
  63. {
  64. if (pause)
  65. {
  66. m_VideoPlayer?.Pause();
  67. }
  68. else
  69. {
  70. m_VideoPlayer?.Play();
  71. }
  72. }
  73. void OnDestroy()
  74. {
  75. if (m_VideoPlayer != null)
  76. {
  77. m_VideoPlayer?.Pause();
  78. m_VideoPlayer?.Release();
  79. m_VideoPlayer = null;
  80. GameObject.Destroy(current.gameObject);
  81. }
  82. }
  83. }
  84. }