Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Video How to start a video by desired time

Discussion in 'Audio & Video' started by sivas123, Apr 28, 2023.

  1. sivas123

    sivas123

    Joined:
    Mar 10, 2023
    Posts:
    3
    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 ,

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Video;
    4. using System;
    5. using System.Collections;
    6. using System.Globalization;
    7.  
    8. public class VideoPlayerController : MonoBehaviour
    9. {
    10.     public string[] videoURLs;
    11.     public RenderTexture renderTexture;
    12.     public Material videoMaterial;
    13.     public float emissionIntensity = 1f; // Adjust this to control the intensity of the emission
    14.     private VideoPlayer videoPlayer;
    15.     private int currentVideoIndex = 0;
    16.     private DateTime desiredStartTime;
    17.     private DateTime currentTime;
    18.     private bool isPlaying = false;
    19.  
    20.     IEnumerator Start()
    21.     {
    22.         UnityWebRequest www = UnityWebRequest.Get("http://worldtimeapi.org/api/timezone/Etc/UTC");
    23.         yield return www.SendWebRequest();
    24.  
    25.         if (www.result != UnityWebRequest.Result.Success)
    26.         {
    27.             Debug.Log("Failed to get current time: " + www.error);
    28.             yield break;
    29.         }
    30.  
    31.         string dateTimeString = ExtractDateTimeStringFromJson(www.downloadHandler.text);
    32.  
    33.         DateTime.TryParseExact(dateTimeString, "yyyy-MM-ddTHH:mm:ss.ffffffZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out currentTime);
    34.  
    35.         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
    36.  
    37.         TimeSpan delayTime = desiredStartTime - currentTime;
    38.  
    39.         if (delayTime.TotalSeconds > 0)
    40.         {
    41.             yield return new WaitForSeconds((float)delayTime.TotalSeconds);
    42.         }
    43.  
    44.         videoPlayer = gameObject.AddComponent<VideoPlayer>();
    45.         videoPlayer.source = VideoSource.Url;
    46.         videoPlayer.url = videoURLs[currentVideoIndex];
    47.         videoPlayer.playOnAwake = false;
    48.  
    49.         renderTexture = new RenderTexture(1920, 1080, 0);
    50.         videoPlayer.targetTexture = renderTexture;
    51.  
    52.         videoMaterial.SetTexture("_EmissionMap", renderTexture); // Set the render texture as the emission map
    53.         videoMaterial.EnableKeyword("_EMISSION"); // Enable the emission keyword for the material
    54.  
    55.         videoPlayer.loopPointReached += OnVideoEnd;
    56.  
    57.         videoPlayer.Play();
    58.  
    59.         isPlaying = true;
    60.     }
    61.  
    62.     void Update()
    63.     {
    64.         if (Input.GetKeyDown(KeyCode.Space))
    65.         {
    66.             videoPlayer.Play();
    67.         }
    68.         if (Input.GetKeyDown(KeyCode.Escape))
    69.         {
    70.             videoPlayer.Stop();
    71.         }
    72.     }
    73.  
    74.     void OnVideoEnd(VideoPlayer vp)
    75.     {
    76.         currentVideoIndex++;
    77.         if (currentVideoIndex < videoURLs.Length)
    78.         {
    79.             videoPlayer.url = videoURLs[currentVideoIndex];
    80.             videoPlayer.Play();
    81.         }
    82.         else
    83.         {
    84.             Debug.Log("All videos played.");
    85.         }
    86.     }
    87.  
    88.     void OnRenderImage(RenderTexture src, RenderTexture dest)
    89.     {
    90.         if (isPlaying)
    91.         {
    92.             // Apply the video texture to the material
    93.             videoMaterial.SetTexture("_MainTex", src);
    94.  
    95.             // Apply the emission to the material
    96.             Color emissionColor = Color.white * emissionIntensity;
    97.             videoMaterial.SetColor("_EmissionColor", emissionColor);
    98.  
    99.             Graphics.Blit(src, dest, videoMaterial);
    100.         }
    101.         else
    102.         {
    103.             Graphics.Blit(src, dest);
    104.         }
    105.     }
    106.  
    107.     private string ExtractDateTimeStringFromJson(string jsonString)
    108.     {
    109.         int start = jsonString.IndexOf("\"datetime\": \"") + 13;
    110.         int end = start + 26;
    111.         return jsonString.Substring(start, end - start);
    112.     }
    113. }
     
    Last edited: Apr 28, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,580
    Please use code-tags when posting code and not plain text.
     
  3. sivas123

    sivas123

    Joined:
    Mar 10, 2023
    Posts:
    3
    ok .. how to edit the thread ?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,580
    Can you not see the Edit button on your post?
     
  5. sivas123

    sivas123

    Joined:
    Mar 10, 2023
    Posts:
    3
    done