forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideoSource.cs
More file actions
116 lines (100 loc) · 2.92 KB
/
Copy pathVideoSource.cs
File metadata and controls
116 lines (100 loc) · 2.92 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
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
using System;
using System.Collections;
using System.Linq;
using UnityEngine;
using UnityEngine.Video;
namespace Mediapipe.Unity
{
public class VideoSource : ImageSource
{
[SerializeField] private VideoClip[] _availableSources;
private VideoClip _video;
private VideoClip video
{
get
{
if (_video == null && _availableSources != null && _availableSources.Length > 0)
{
video = _availableSources[0];
}
return _video;
}
set
{
_video = value;
resolution = new ResolutionStruct((int)_video.width, (int)_video.height, _video.frameRate);
}
}
private VideoPlayer _videoPlayer;
public override string sourceName => video != null ? video.name : null;
public override string[] sourceCandidateNames => _availableSources?.Select(source => source.name).ToArray();
public override ResolutionStruct[] availableResolutions => video == null ? null : new ResolutionStruct[] { new ResolutionStruct((int)video.width, (int)video.height, video.frameRate) };
public override bool isPlaying => _videoPlayer != null && _videoPlayer.isPlaying;
public override bool isPrepared => _videoPlayer != null && _videoPlayer.isPrepared;
public override void SelectSource(int sourceId)
{
if (sourceId < 0 || sourceId >= _availableSources.Length)
{
throw new ArgumentException($"Invalid source ID: {sourceId}");
}
video = _availableSources[sourceId];
if (_videoPlayer != null)
{
_videoPlayer.clip = video;
}
}
public override IEnumerator Play()
{
if (video == null)
{
throw new InvalidOperationException("Video is not selected");
}
_videoPlayer = gameObject.AddComponent<VideoPlayer>();
_videoPlayer.renderMode = VideoRenderMode.APIOnly;
_videoPlayer.isLooping = true;
_videoPlayer.clip = video;
_videoPlayer.Prepare();
yield return new WaitUntil(() => _videoPlayer.isPrepared);
_videoPlayer.Play();
}
public override IEnumerator Resume()
{
if (!isPrepared)
{
throw new InvalidOperationException("VideoPlayer is not prepared");
}
if (!isPlaying)
{
_videoPlayer.Play();
}
yield return null;
}
public override void Pause()
{
if (!isPlaying)
{
return;
}
_videoPlayer.Pause();
}
public override void Stop()
{
if (_videoPlayer == null)
{
return;
}
_videoPlayer.Stop();
Destroy(gameObject.GetComponent<VideoPlayer>());
_videoPlayer = null;
}
public override Texture GetCurrentTexture()
{
return _videoPlayer != null ? _videoPlayer.texture : null;
}
}
}