Snippets
Created by
Michael Allison
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | //MediaPlayerControlBehavior.cs
// You need to define AVPRO_PACKAGE_TIMELINE manually to use this script
// We could set up the asmdef to reference the package, but the package doesn't
// existing in Unity 2017 etc, and it throws an error due to missing reference
//#define AVPRO_PACKAGE_TIMELINE
#if (UNITY_2018_1_OR_NEWER && AVPRO_PACKAGE_TIMELINE)
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using System.Collections.Generic;
using System.Reflection;
//-----------------------------------------------------------------------------
// Copyright 2020-2021 RenderHeads Ltd. All rights reserved.
//-----------------------------------------------------------------------------
namespace RenderHeads.Media.AVProVideo.Playables
{
public class MediaPlayerControlBehaviour : PlayableBehaviour
{
public MediaPlayer mediaPlayer = null;
public MediaReference mediaReference = null;
public float audioVolume = 1f;
public double startTime = -1.0;
public bool pauseOnEnd = true;
public bool seekEveryFrame = false;
public MethodInfo stopRenderCoroutine = null;
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
if (mediaPlayer != null)
{
if (Application.isPlaying)
{
if (mediaReference != null && mediaReference != mediaPlayer.MediaReference)
{
mediaPlayer.OpenMedia(mediaReference, true);
if (mediaPlayer.Control != null)
{
if (seekEveryFrame)
{
mediaPlayer.Control.Seek(startTime);
WaitForFinishSeek();
}
else
{
mediaPlayer.Control.SeekFast(startTime);
mediaPlayer.Play();
}
}
}
else
{
if (mediaPlayer.Control != null)
{
if(seekEveryFrame)
{
mediaPlayer.Control.Seek(startTime);
WaitForFinishSeek();
}
else
{
mediaPlayer.Control.SeekFast(startTime);
}
}
if(!seekEveryFrame) mediaPlayer.Play();
}
}
}
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
if (mediaPlayer != null)
{
if (pauseOnEnd)
{
mediaPlayer.Pause();
}
}
}
public override void OnGraphStart(Playable playable)
{
base.OnGraphStart(playable);
if(seekEveryFrame) stopRenderCoroutine?.Invoke(mediaPlayer, null);
}
void WaitForFinishSeek()
{
while (mediaPlayer.Control.IsSeeking())
{
mediaPlayer.Player.Update();
mediaPlayer.Player.EndUpdate();
}
mediaPlayer.Player.Render();
}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
if (mediaPlayer != null)
{
if (mediaPlayer.Control != null)
{
if (seekEveryFrame)
{
mediaPlayer.Control.Seek(startTime > 0 ? startTime + playable.GetTime() : playable.GetTime());
WaitForFinishSeek();
}
}
}
}
}
}
#endif
//MediaPlayerControlAsset.cs
// You need to define AVPRO_PACKAGE_TIMELINE manually to use this script
// We could set up the asmdef to reference the package, but the package doesn't
// existing in Unity 2017 etc, and it throws an error due to missing reference
//#define AVPRO_PACKAGE_TIMELINE
#if (UNITY_2018_1_OR_NEWER && AVPRO_PACKAGE_TIMELINE)
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using System.Collections.Generic;
using System.Reflection;
//-----------------------------------------------------------------------------
// Copyright 2020-2021 RenderHeads Ltd. All rights reserved.
//-----------------------------------------------------------------------------
namespace RenderHeads.Media.AVProVideo.Playables
{
[System.Serializable]
public class MediaPlayerControlAsset : PlayableAsset
{
public Object binding { get; set; }
//public ExposedReference<MediaPlayer> mediaPlayer;
public MediaReference mediaReference;
[Range(0f, 1f)]
public float audioVolume = 1f;
public double startTime = -1.0;
public bool pauseOnEnd = true;
public bool seekEveryFrame = false;
public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
{
var playable = ScriptPlayable<MediaPlayerControlBehaviour>.Create(graph);
var behaviour = playable.GetBehaviour();
//behaviour.mediaPlayer = mediaPlayer.Resolve(graph.GetResolver());
behaviour.audioVolume = audioVolume;
behaviour.pauseOnEnd = pauseOnEnd;
behaviour.startTime = startTime;
behaviour.mediaReference = mediaReference;
behaviour.mediaPlayer = (MediaPlayer)binding;
behaviour.seekEveryFrame = seekEveryFrame;
behaviour.stopRenderCoroutine = behaviour.mediaPlayer.GetType().GetMethod("StopRenderCoroutine", BindingFlags.NonPublic | BindingFlags.Instance);
return playable;
}
}
}
#endif
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.