MediaPlayerControlMixerBehaviour.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 MediaPlayerControlMixerBehaviour : PlayableBehaviour
  16. {
  17. public float audioVolume = 1f;
  18. public string videoPath = null;
  19. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  20. {
  21. MediaPlayer mediaPlayer = playerData as MediaPlayer;
  22. float finalVolume = 0f;
  23. if (!mediaPlayer)
  24. return;
  25. int inputCount = playable.GetInputCount(); //get the number of all clips on this track
  26. for (int i = 0; i < inputCount; i++)
  27. {
  28. float inputWeight = playable.GetInputWeight(i);
  29. ScriptPlayable<MediaPlayerControlBehaviour> inputPlayable = (ScriptPlayable<MediaPlayerControlBehaviour>)playable.GetInput(i);
  30. MediaPlayerControlBehaviour input = inputPlayable.GetBehaviour();
  31. // Use the above variables to process each frame of this playable.
  32. finalVolume += input.audioVolume * inputWeight;
  33. }
  34. if (mediaPlayer != null)
  35. {
  36. mediaPlayer.AudioVolume = finalVolume;
  37. if (mediaPlayer.Control != null)
  38. {
  39. mediaPlayer.Control.SetVolume(finalVolume);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. #endif