315 lines
8.3 KiB
C#
315 lines
8.3 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Video;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using System.IO;
|
|
using UnityEngine.Networking;
|
|
|
|
public class VideoController : MonoBehaviour
|
|
{
|
|
|
|
VideoPlayer video;
|
|
|
|
public Slider videoTime;
|
|
|
|
public Sprite pause, unpause;
|
|
public Image buttonPause;
|
|
|
|
Animator thisAnime;
|
|
|
|
Text txtcache;
|
|
Cache newcache; bool freeCache = false;
|
|
|
|
bool byDownload = false;
|
|
bool byPlay = false;
|
|
// Start is called before the first frame update
|
|
|
|
IEnumerator secondStart(string publishId)
|
|
{
|
|
thisAnime = GetComponent<Animator>();
|
|
video = GetComponent<VideoPlayer>();
|
|
video.targetCamera = Camera.main;
|
|
|
|
// Log inicial
|
|
Debug.Log("[com.myrobot.AppClass] - Iniciando secondStart para o ID: " + publishId);
|
|
|
|
// Obtém o URL do vídeo do Vimeo
|
|
yield return StartCoroutine(GetVimeoVideoUrl(publishId, OnVimeoUrlReceived));
|
|
|
|
txtcache = GetComponentInChildren<Text>();
|
|
|
|
yield break;
|
|
}
|
|
|
|
private void OnVimeoUrlReceived(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
Debug.LogError("[com.myrobot.AppClass] - Falha ao obter o URL do vídeo do Vimeo");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("[com.myrobot.AppClass] - URL do vídeo obtida: " + url);
|
|
|
|
// Inicia o download usando uma Coroutine
|
|
StartCoroutine(DownloadAndPlayVideo(url));
|
|
//PlayVideoFromURL(url);
|
|
}
|
|
|
|
public void fakeStart(int idmedia, string publishId)
|
|
{
|
|
StartCoroutine(secondStart(publishId));
|
|
//StartCoroutine(secondStart(idmedia));
|
|
}
|
|
|
|
IEnumerator checkPause()
|
|
{
|
|
while (true)
|
|
{
|
|
if (video.isPlaying)
|
|
{
|
|
buttonPause.sprite = pause;
|
|
}
|
|
else
|
|
{
|
|
buttonPause.sprite = unpause;
|
|
}
|
|
|
|
yield return new WaitForSecondsRealtime(.25f);
|
|
}
|
|
}
|
|
|
|
|
|
IEnumerator setvideoTime()
|
|
{
|
|
while (!video.isPrepared)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
video.Play();
|
|
|
|
StartCoroutine(checkPause());
|
|
|
|
videoTime = GetComponentInChildren<Slider>();
|
|
videoTime.maxValue = Convert.ToSingle(video.length);
|
|
while (true)
|
|
{
|
|
videoTime.value = Convert.ToSingle(video.time);
|
|
yield return new WaitForSecondsRealtime(1);
|
|
}
|
|
}
|
|
|
|
public bool endVideo = false;
|
|
|
|
|
|
IEnumerator checkEnd()
|
|
{
|
|
|
|
while (!endVideo)
|
|
{
|
|
video.loopPointReached += EndReached;
|
|
yield return new WaitForFixedUpdate();
|
|
}
|
|
|
|
thisAnime.enabled = true;
|
|
thisAnime.SetTrigger("end");
|
|
|
|
GeneralScript general = GeneralScript.instance;
|
|
general.LiberarAvanco(true);
|
|
|
|
yield return new WaitForSecondsRealtime(1);
|
|
|
|
RectTransform rectVideo = thisAnime.gameObject.GetComponent<RectTransform>();
|
|
Vector2 rect = rectVideo.anchoredPosition;
|
|
|
|
//thisAnime.enabled = false;
|
|
yield return new WaitForEndOfFrame();
|
|
rectVideo.anchoredPosition = rect;
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
video.Stop();
|
|
|
|
yield break;
|
|
}
|
|
|
|
void EndReached(UnityEngine.Video.VideoPlayer vp)
|
|
{
|
|
endVideo = true;
|
|
}
|
|
|
|
public void pauseVideo()
|
|
{
|
|
if (video.isPlaying)
|
|
{
|
|
video.Pause();
|
|
}
|
|
else
|
|
{
|
|
video.Play();
|
|
}
|
|
}
|
|
|
|
public void changeValue()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (video.canSetTime)
|
|
{
|
|
video.time = videoTime.value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowHide()
|
|
{
|
|
bool actualstate = thisAnime.GetBool("show");
|
|
thisAnime.SetBool("show", !actualstate);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private string vimeoAccessToken = "ce80fee63e837410e3819951b65fedb7";
|
|
|
|
public IEnumerator GetVimeoVideoUrl(string videoId, Action<string> onVideoUrlReceived)
|
|
{
|
|
string vimeoApiUrl = $"https://api.vimeo.com{videoId}";
|
|
|
|
UnityWebRequest www = UnityWebRequest.Get(vimeoApiUrl);
|
|
www.SetRequestHeader("Authorization", "Bearer " + vimeoAccessToken);
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.LogError("[com.myrobot.AppClass] - Erro ao acessar a API do Vimeo: " + www.error);
|
|
onVideoUrlReceived(null); // Passa null se ocorrer um erro
|
|
}
|
|
else
|
|
{
|
|
// Parse the JSON response to extract the direct video link
|
|
string jsonResponse = www.downloadHandler.text;
|
|
Debug.Log("[com.myrobot.AppClass] - Resposta da API do Vimeo: " + jsonResponse);
|
|
|
|
// Aqui você pode usar uma biblioteca JSON para extrair o link direto do vídeo
|
|
string directVideoUrl = ExtractDirectVideoUrlFromJson(jsonResponse);
|
|
|
|
onVideoUrlReceived(directVideoUrl); // Passa o link do vídeo
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private string ExtractDirectVideoUrlFromJson(string jsonResponse)
|
|
{
|
|
// Use uma biblioteca JSON como JSON.net ou o próprio JsonUtility do Unity
|
|
// para parsear a resposta e extrair o link direto do vídeo.
|
|
|
|
// Exemplo com JsonUtility:
|
|
VimeoResponse vimeoResponse = JsonUtility.FromJson<VimeoResponse>(jsonResponse);
|
|
string linkVideo = vimeoResponse?.files?[0]?.link; // Exemplo para extrair o link do arquivo de vídeo
|
|
Debug.Log("[com.myrobot.AppClass] - Link recebido pelo Vimeo: " + linkVideo);
|
|
return linkVideo;
|
|
}
|
|
|
|
|
|
void PlayVideoFromURL(string videoUrl)
|
|
{
|
|
VideoPlayer videoPlayer = GetComponent<VideoPlayer>();
|
|
|
|
Debug.Log($"[com.myrobot.AppClass] - Iniciando reprodução do vídeo diretamente do link: {videoUrl}");
|
|
|
|
videoPlayer.source = VideoSource.Url;
|
|
videoPlayer.url = videoUrl;
|
|
videoPlayer.Prepare();
|
|
|
|
videoPlayer.prepareCompleted += (vp) =>
|
|
{
|
|
Debug.Log("[com.myrobot.AppClass] - Vídeo preparado. Reproduzindo...");
|
|
vp.Play();
|
|
|
|
StartCoroutine(setvideoTime());
|
|
StartCoroutine(checkEnd());
|
|
};
|
|
|
|
videoPlayer.errorReceived += (vp, message) =>
|
|
{
|
|
Debug.LogError($"[com.myrobot.AppClass] - Erro ao reproduzir o vídeo: {message}");
|
|
|
|
StartCoroutine(DownloadAndPlayVideo(videoUrl));
|
|
};
|
|
}
|
|
|
|
IEnumerator DownloadAndPlayVideo(string videoUrl)
|
|
{
|
|
Debug.Log($"[com.myrobot.AppClass] - Tentando iniciar download do vídeo: {videoUrl}");
|
|
string tempFilePath = Path.Combine(Application.temporaryCachePath, "tempVideo.mp4");
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(videoUrl))
|
|
{
|
|
// Ignorar validação de certificados
|
|
www.certificateHandler = new BypassCertificateHandler();
|
|
www.downloadHandler = new DownloadHandlerFile(tempFilePath);
|
|
|
|
Debug.Log($"[com.myrobot.AppClass] - Iniciando download: {videoUrl}");
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isHttpError || www.isNetworkError)
|
|
{
|
|
Debug.LogError($"[com.myrobot.AppClass] - Erro ao baixar o vídeo: {www.error}");
|
|
|
|
PlayVideoFromURL(videoUrl);
|
|
yield break;
|
|
}
|
|
|
|
Debug.Log($"[com.myrobot.AppClass] - Vídeo baixado com sucesso: {tempFilePath}");
|
|
|
|
// Reproduzir o vídeo baixado
|
|
PlayVideo(tempFilePath, videoUrl);
|
|
}
|
|
}
|
|
|
|
private void PlayVideo(string filePath, string url)
|
|
{
|
|
var videoPlayer = GetComponent<VideoPlayer>();
|
|
videoPlayer.url = new Uri(filePath).AbsoluteUri;
|
|
|
|
videoPlayer.Prepare();
|
|
videoPlayer.prepareCompleted += (vp) =>
|
|
{
|
|
Debug.Log("[com.myrobot.AppClass] - Vídeo preparado. Reproduzindo...");
|
|
vp.Play();
|
|
|
|
StartCoroutine(setvideoTime());
|
|
StartCoroutine(checkEnd());
|
|
};
|
|
|
|
videoPlayer.errorReceived += (vp, message) =>
|
|
{
|
|
Debug.LogError($"[com.myrobot.AppClass] - Erro no VideoPlayer: {message}");
|
|
|
|
PlayVideoFromURL(url);
|
|
};
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class VimeoResponse
|
|
{
|
|
public VimeoFile[] files;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class VimeoFile
|
|
{
|
|
public string link;
|
|
} |