i try to get a current time from my server as per tutorial , and if server time and my desired time is equal or greater video should play… but after setup that code video is not start playing when desired time match with server time
Here is the code ,
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Video;
using System;
using System.Collections;
using System.Globalization;
public class VideoPlayerController : MonoBehaviour
{
public string[] videoURLs;
public RenderTexture renderTexture;
public Material videoMaterial;
public float emissionIntensity = 1f; // Adjust this to control the intensity of the emission
private VideoPlayer videoPlayer;
private int currentVideoIndex = 0;
private DateTime desiredStartTime;
private DateTime currentTime;
private bool isPlaying = false;
IEnumerator Start()
{
UnityWebRequest www = UnityWebRequest.Get("http://worldtimeapi.org/api/timezone/Etc/UTC");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log("Failed to get current time: " + www.error);
yield break;
}
string dateTimeString = ExtractDateTimeStringFromJson(www.downloadHandler.text);
DateTime.TryParseExact(dateTimeString, "yyyy-MM-ddTHH:mm:ss.ffffffZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out currentTime);
DateTime.TryParseExact("28-04-2023 09:38:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out desiredStartTime); // Set desired start time here
TimeSpan delayTime = desiredStartTime - currentTime;
if (delayTime.TotalSeconds > 0)
{
yield return new WaitForSeconds((float)delayTime.TotalSeconds);
}
videoPlayer = gameObject.AddComponent<VideoPlayer>();
videoPlayer.source = VideoSource.Url;
videoPlayer.url = videoURLs[currentVideoIndex];
videoPlayer.playOnAwake = false;
renderTexture = new RenderTexture(1920, 1080, 0);
videoPlayer.targetTexture = renderTexture;
videoMaterial.SetTexture("_EmissionMap", renderTexture); // Set the render texture as the emission map
videoMaterial.EnableKeyword("_EMISSION"); // Enable the emission keyword for the material
videoPlayer.loopPointReached += OnVideoEnd;
videoPlayer.Play();
isPlaying = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
videoPlayer.Play();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
videoPlayer.Stop();
}
}
void OnVideoEnd(VideoPlayer vp)
{
currentVideoIndex++;
if (currentVideoIndex < videoURLs.Length)
{
videoPlayer.url = videoURLs[currentVideoIndex];
videoPlayer.Play();
}
else
{
Debug.Log("All videos played.");
}
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (isPlaying)
{
// Apply the video texture to the material
videoMaterial.SetTexture("_MainTex", src);
// Apply the emission to the material
Color emissionColor = Color.white * emissionIntensity;
videoMaterial.SetColor("_EmissionColor", emissionColor);
Graphics.Blit(src, dest, videoMaterial);
}
else
{
Graphics.Blit(src, dest);
}
}
private string ExtractDateTimeStringFromJson(string jsonString)
{
int start = jsonString.IndexOf("\"datetime\": \"") + 13;
int end = start + 26;
return jsonString.Substring(start, end - start);
}
}