-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMediaPlayerControlBehaviour.cs
More file actions
141 lines (125 loc) · 4.48 KB
/
MediaPlayerControlBehaviour.cs
File metadata and controls
141 lines (125 loc) · 4.48 KB
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
// 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;
using System.Diagnostics;
//-----------------------------------------------------------------------------
// 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 = 0.0;
public bool pauseOnEnd = true;
public bool frameAccurateSeek = false;
public MethodInfo stopRenderCoroutine = null;
public double preloadTime = 0.3;
private bool _preparing = false;
void DoSeek(double time)
{
if (frameAccurateSeek)
{
mediaPlayer.Control.Seek(startTime + time);
WaitForFinishSeek();
}
else
{
mediaPlayer.Control.SeekFast(startTime + time);
}
}
public void PrepareMedia()
{
if (mediaPlayer == null) return;
if (mediaPlayer.MediaOpened || _preparing)
return;
if (mediaPlayer.MediaSource == MediaSource.Reference ? mediaPlayer.MediaReference != null : mediaPlayer.MediaPath.Path != "")
{
if(mediaReference != null && mediaReference != mediaPlayer.MediaReference)
{
mediaPlayer.OpenMedia(mediaReference, false);
}
else
{
mediaPlayer.OpenMedia(mediaPlayer.MediaReference, false);
}
if (frameAccurateSeek) stopRenderCoroutine?.Invoke(mediaPlayer, null);
_preparing = true;
}
}
public void StopMedia()
{
if(mediaPlayer != null && mediaPlayer.Control != null)
{
mediaPlayer.Control.Stop();
}
_preparing = false;
}
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
if (mediaPlayer != null)
{
if (mediaPlayer.Control != null)
{
DoSeek(playable.GetTime());
}
mediaPlayer.Play();
if (!Application.isPlaying || frameAccurateSeek)
mediaPlayer.Pause();
}
_preparing = false;
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
if (mediaPlayer != null)
{
if (pauseOnEnd)
{
mediaPlayer.Pause();
}
}
_preparing = false;
}
void WaitForFinishSeek()
{
Stopwatch timer = Stopwatch.StartNew(); //never get stuck
while (mediaPlayer.Control.IsSeeking() && timer.ElapsedMilliseconds < 5000)
{
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 (frameAccurateSeek)
{
mediaPlayer.Control.Seek(startTime + playable.GetTime());
WaitForFinishSeek();
}
else if(!Application.isPlaying)
{
mediaPlayer.Control.SeekFast(startTime + playable.GetTime());
mediaPlayer.Player.Update();
mediaPlayer.Player.EndUpdate();
mediaPlayer.Player.Render();
}
}
}
}
}
}
#endif