| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Components.Video.Plugin |
| | 5 | | { |
| | 6 | | public class WebVideoPlayer : IDisposable |
| | 7 | | { |
| 0 | 8 | | public Texture2D texture { private set; get; } |
| 0 | 9 | | public float volume { private set; get; } |
| 1 | 10 | | public bool playing => GetState() == VideoState.PLAYING; |
| 677 | 11 | | public bool isError => GetState() == VideoState.ERROR; |
| 31 | 12 | | public bool visible { get; set; } = true; |
| 0 | 13 | | public bool isReady { get; private set; } = false; |
| | 14 | |
|
| | 15 | | public readonly string url; |
| | 16 | |
|
| | 17 | | private string videoPlayerId; |
| | 18 | |
|
| | 19 | | private readonly IVideoPluginWrapper plugin; |
| | 20 | |
|
| | 21 | | private bool playWhenReady = false; |
| 31 | 22 | | private float playStartTime = -1; |
| | 23 | |
|
| 31 | 24 | | private string lastError = ""; |
| | 25 | |
|
| 31 | 26 | | public WebVideoPlayer(string id, string url, bool useHls, IVideoPluginWrapper plugin) |
| | 27 | | { |
| 31 | 28 | | videoPlayerId = id; |
| 31 | 29 | | this.plugin = plugin; |
| 31 | 30 | | this.url = url; |
| 31 | 31 | | plugin.Create(id, url, useHls); |
| 31 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Update() |
| | 35 | | { |
| 77 | 36 | | switch (plugin.GetState(videoPlayerId)) |
| | 37 | | { |
| | 38 | | case VideoState.ERROR: |
| 2 | 39 | | string newError = plugin.GetError(videoPlayerId); |
| | 40 | |
|
| 2 | 41 | | if ( newError != lastError ) |
| | 42 | | { |
| 2 | 43 | | lastError = newError; |
| 2 | 44 | | Debug.LogError(lastError); |
| | 45 | | } |
| | 46 | |
|
| 2 | 47 | | break; |
| | 48 | | case VideoState.READY: |
| 19 | 49 | | if (!isReady) |
| | 50 | | { |
| 19 | 51 | | isReady = true; |
| | 52 | |
|
| 19 | 53 | | texture = plugin.PrepareTexture(videoPlayerId); |
| | 54 | | } |
| | 55 | |
|
| 19 | 56 | | if (playWhenReady) |
| | 57 | | { |
| 1 | 58 | | PlayInternal(); |
| 1 | 59 | | playWhenReady = false; |
| | 60 | | } |
| | 61 | |
|
| 1 | 62 | | break; |
| | 63 | | case VideoState.PLAYING: |
| 2 | 64 | | if (visible) |
| 0 | 65 | | plugin.TextureUpdate(videoPlayerId); |
| | 66 | |
|
| | 67 | | break; |
| | 68 | | } |
| 53 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Play() |
| | 72 | | { |
| 11 | 73 | | if (isError) |
| 1 | 74 | | return; |
| | 75 | |
|
| 10 | 76 | | if (!isReady) |
| | 77 | | { |
| 3 | 78 | | playWhenReady = true; |
| 3 | 79 | | return; |
| | 80 | | } |
| | 81 | |
|
| 7 | 82 | | PlayInternal(); |
| 7 | 83 | | } |
| | 84 | |
|
| | 85 | | private void PlayInternal() |
| | 86 | | { |
| 8 | 87 | | plugin.Play(videoPlayerId, playStartTime); |
| 8 | 88 | | playStartTime = -1; |
| 8 | 89 | | } |
| | 90 | |
|
| | 91 | | public void Pause() |
| | 92 | | { |
| 16 | 93 | | if (isError) |
| 1 | 94 | | return; |
| | 95 | |
|
| 15 | 96 | | playStartTime = plugin.GetTime(videoPlayerId); |
| 15 | 97 | | plugin.Pause(videoPlayerId); |
| 15 | 98 | | playWhenReady = false; |
| 15 | 99 | | } |
| | 100 | |
|
| | 101 | | public void SetVolume(float volume) |
| | 102 | | { |
| 54 | 103 | | if (isError) |
| 1 | 104 | | return; |
| | 105 | |
|
| 53 | 106 | | plugin.SetVolume(videoPlayerId, volume); |
| 53 | 107 | | this.volume = volume; |
| 53 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetTime(float timeSecs) |
| | 111 | | { |
| 3 | 112 | | if (isError) |
| 1 | 113 | | return; |
| | 114 | |
|
| 2 | 115 | | playStartTime = timeSecs; |
| 2 | 116 | | plugin.SetTime(videoPlayerId, timeSecs); |
| 2 | 117 | | } |
| | 118 | |
|
| | 119 | | public void SetLoop(bool loop) |
| | 120 | | { |
| 18 | 121 | | if (isError) |
| 1 | 122 | | return; |
| | 123 | |
|
| 17 | 124 | | plugin.SetLoop(videoPlayerId, loop); |
| 17 | 125 | | } |
| | 126 | |
|
| | 127 | | public void SetPlaybackRate(float playbackRate) |
| | 128 | | { |
| 17 | 129 | | if (isError) |
| 1 | 130 | | return; |
| | 131 | |
|
| 16 | 132 | | plugin.SetPlaybackRate(videoPlayerId, playbackRate); |
| 16 | 133 | | } |
| | 134 | |
|
| | 135 | | public float GetTime() |
| | 136 | | { |
| 47 | 137 | | if (isError) |
| 1 | 138 | | return 0; |
| | 139 | |
|
| 46 | 140 | | return plugin.GetTime(videoPlayerId); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | public float GetDuration() |
| | 144 | | { |
| 48 | 145 | | if (isError) |
| 1 | 146 | | return 0; |
| | 147 | |
|
| 47 | 148 | | float duration = plugin.GetDuration(videoPlayerId); |
| | 149 | |
|
| 47 | 150 | | if (float.IsNaN(duration)) |
| 1 | 151 | | duration = -1; |
| | 152 | |
|
| 47 | 153 | | return duration; |
| | 154 | | } |
| | 155 | |
|
| | 156 | | public VideoState GetState() |
| | 157 | | { |
| 1253 | 158 | | return (VideoState)plugin.GetState(videoPlayerId); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | public void Dispose() |
| | 162 | | { |
| 2 | 163 | | plugin.Remove(videoPlayerId); |
| 2 | 164 | | } |
| | 165 | | } |
| | 166 | | } |