MediaPlayerControlBehaviour.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // You need to define AVPRO_PACKAGE_TIMELINE manually to use this script
  2. // We could set up the asmdef to reference the package, but the package doesn't
  3. // existing in Unity 2017 etc, and it throws an error due to missing reference
  4. //#define AVPRO_PACKAGE_TIMELINE
  5. #if (UNITY_2018_1_OR_NEWER && AVPRO_PACKAGE_TIMELINE)
  6. using UnityEngine;
  7. using UnityEngine.Playables;
  8. using UnityEngine.Timeline;
  9. using System.Collections.Generic;
  10. //-----------------------------------------------------------------------------
  11. // Copyright 2020-2021 RenderHeads Ltd. All rights reserved.
  12. //-----------------------------------------------------------------------------
  13. namespace RenderHeads.Media.AVProVideo.Playables
  14. {
  15. public class MediaPlayerControlBehaviour : PlayableBehaviour
  16. {
  17. public MediaPlayer mediaPlayer = null;
  18. public MediaReference mediaReference = null;
  19. public float audioVolume = 1f;
  20. public double startTime = -1.0;
  21. public bool pauseOnEnd = true;
  22. public override void OnBehaviourPlay(Playable playable, FrameData info)
  23. {
  24. if (mediaPlayer != null)
  25. {
  26. if (Application.isPlaying)
  27. {
  28. if (mediaReference != null && mediaReference != mediaPlayer.MediaReference)
  29. {
  30. mediaPlayer.OpenMedia(mediaReference, true);
  31. if (mediaPlayer.Control != null)
  32. {
  33. mediaPlayer.Control.SeekFast(startTime);
  34. }
  35. }
  36. else
  37. {
  38. mediaPlayer.Play();
  39. }
  40. }
  41. }
  42. }
  43. public override void OnBehaviourPause(Playable playable, FrameData info)
  44. {
  45. if (mediaPlayer != null)
  46. {
  47. if (pauseOnEnd)
  48. {
  49. mediaPlayer.Pause();
  50. }
  51. }
  52. }
  53. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  54. {
  55. }
  56. }
  57. }
  58. #endif