Search Unity

Scroll through video array randomly

Discussion in 'Scripting' started by dlstilts, Mar 19, 2018.

  1. dlstilts

    dlstilts

    Joined:
    Sep 29, 2017
    Posts:
    30
    HI I am trying to randomly cycle through an array of videos. I just want the the videos to play from start and randomly go through the video array one after another forever. I have tried to adopt the code from an audio tutorial but am having no luck with it working for video. I am rather new to this (sorry if the code is way off). Please help. Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5.  
    6. public class RANDOM2 : MonoBehaviour
    7. {
    8.     private int videoClipIndex;
    9.     public VideoClip[] VideoClipArray;
    10.     private VideoPlayer videoPlayer;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         videoPlayer = GetComponent<VideoPlayer>();
    16.         videoPlayer.Pause();
    17.     }
    18.     void Start()
    19.     {
    20.         videoPlayer.targetTexture.Release();
    21.         videoPlayer.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)];
    22.         videoPlayer.Play();
    23.         SetNextClip();
    24.     }
    25.  
    26.    
    27.     void Update()
    28.     {
    29.      
    30.     }
    31.      public void SetNextClip()
    32.     {
    33.     videoClipIndex++;
    34.  
    35.  
    36.     if (videoClipIndex >= VideoClipArray.Length)
    37.     {
    38.      videoClipIndex = videoClipIndex % VideoClipArray.Length;
    39.      }
    40.  
    41.      videoPlayer.clip = VideoClipArray[videoClipIndex];
    42.  
    43.      videoPlayer.Play();
    44.  
    45.      }
    46. }
    47.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    792
    All of the code in your Start method, minus the SetNextClip method call, should do exactly what you want.
    The SetNextClip method and videoClipIndex variable are not needed. All you really have to do is take the code from the Start method and place it in a new method that can be called when the current video ends, which you can determine by getting the length of the video clip and using Time.time to set how much time should pass before the next video plays.

    Something like:
    Code (CSharp):
    1.  
    2. public class RANDOM2 : MonoBehavior {
    3.  
    4.    public VideoClip[] VideoClipArray;
    5.    private VideoPlayer videoPlayer;
    6.    private float timeUntilNextVideo;
    7.  
    8.    void Awake() {
    9.       videoPlayer = GetComponent<VideoPlayer>();
    10.       videoPlayer.Pause();
    11.    }
    12.  
    13.    void Start() {
    14.       timeUntilNextVideo = 0f;
    15.    }
    16.  
    17.    void Update() {
    18.       if(Time.time > timeUntilNextVideo) {
    19.          PlayRandomVideo();
    20.       }
    21.    }
    22.  
    23.    void PlayRandomVideo() {
    24.       videoPlayer.targetTexture.Release();
    25.       videoPlayer.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)];
    26.  
    27.       timeUntilNextVideo = Time.time + videoPlayer.clip.length;
    28.  
    29.       videoPlayer.Play();
    30.    }
    31. }
    32.  
    I'm not currently able to test if this will work, but the general idea is that you will add the length (in seconds) of the video clip to the total time (in seconds) that has passed since the application started running. Once the amount of time the application has been running catches up to this value, it will call the PlayRandomVideo method again and loop the processes endlessly.
     
  3. dlstilts

    dlstilts

    Joined:
    Sep 29, 2017
    Posts:
    30
    Thank you for your answer. However, the line:

    timeUntilNextVideo = Time.time + videoPlayer.clip.length;

    Comes up with an error? Do you know what might be the problem?

    Cheers!
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    792
    What's the error message?
     
  5. dlstilts

    dlstilts

    Joined:
    Sep 29, 2017
    Posts:
    30
    It says:

    Cannot implicitly convert type 'double' to 'float '. An explicit conversation exists (are you missing a cast)

    Thanks.
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    792
    Ah, I didn't realize that the video clip length returned a double instead of a float.
    Simply add "(float)" before videoPlayer.clip.length;

    If you're interested in why this fixes the error, view the spoiler below.
    Time.time is a float and the length of a video clip is a double.
    Doubles and floats are both numbers that are accurate up to a certain number of decimal places, floats being a smaller number of decimal places, and doubles being a larger number of decimal places.
    Since we're trying to add a number with a lot of decimal places to a number that doesn't have that many decimal places, an error occurs.

    You can fix this with a cast (which is what the error is referring to).
    Casting is when you convert one data type into another, and it will work as long as the data between the two values are the same. (IE, you cannot convert letters into numbers).
    You can convert a double into a float just by adding "(float)" before the value, and this will chop off the decimal places of the double that go over the max number of places for a float, like so:
    Code (CSharp):
    1. timeUntilNextVideo = Time.time + (float)videoPlayer.clip.length;
    Casting can be done with other data types as well. Say if you have a float value like this:
    Code (CSharp):
    1. float percent = 36.19362;
    And you only want the whole number, excluding the decimal places. You can cast this value as an integer:
    Code (CSharp):
    1.  
    2. float percent = 36.19362;
    3. int wholePercent = (int)percent;
    4.  
    And the value of "wholePercent" will be 36.

    I recommend going through Unity's tutorials about C# and its API here so you can better understand how to write your own scripts, because there's only so much you can do without scripting.
     
  7. dlstilts

    dlstilts

    Joined:
    Sep 29, 2017
    Posts:
    30
    Thank you. Yes I need to dig more into it. The code has no errors but there is an error within Unity when I try to run?
    The error in Unity is:

    NullReferenceException: Object reference not set to an instance of an object
    RANDOM2.PlayRandomVideo () (at Assets/RANDOM2.cs:39)
    RANDOM2.Update () (at Assets/RANDOM2.cs:33)

    I have googled and tried to figure out with no luck... I will keep trying but if you have any ideas would appreciate.
    Thanks!
     
  8. dlstilts

    dlstilts

    Joined:
    Sep 29, 2017
    Posts:
    30
    Ahh figured it out. Here is the final code if anyone is interested. Thanks for your help!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5.  
    6. public class RANDOM2 : MonoBehaviour
    7. {
    8.  
    9.     public VideoClip[] VideoClipArray;
    10.     private VideoPlayer videoPlayer;
    11.     private float timeUntilNextVideo;
    12.  
    13.     void Awake()
    14.     {
    15.         videoPlayer = GetComponent<VideoPlayer>();
    16.         videoPlayer.Pause();
    17.     }
    18.     void Start()
    19.     {
    20.       timeUntilNextVideo = 0f;
    21.  
    22.    
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.    if (Time.time > timeUntilNextVideo)
    29.        {
    30.            
    31.              videoPlayer.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)];
    32.  
    33.  
    34.             timeUntilNextVideo = Time.time + (float)videoPlayer.clip.length;
    35.  
    36.              videoPlayer.Play();
    37.         }
    38.     }
    39.  
    40. }
    41.  
    42.  
     
    buenacolombia likes this.
  9. kf_unity44

    kf_unity44

    Joined:
    Sep 8, 2019
    Posts:
    1
    Hi sorry but does the videos transition without a break? Mine don't do you know how to fix that?
     
  10. richardzzzarnold

    richardzzzarnold

    Joined:
    Aug 2, 2012
    Posts:
    87
    you need to make two videoPlayers and alternate between the two of them and change the script to this:
    • Code (CSharp):
      1. public class VideoRandomizer : MonoBehaviour
      2. {
      3.     public VideoClip[] VideoClipArray;
      4.     public VideoPlayer videoPlayer;
      5.     public VideoPlayer videoPlayer2;
      6.  
      7.     private float timeUntilNextVideo;
      8.     public bool assignedPlayer;
      9.     void Awake()
      10.     {
      11.       //  videoPlayer = GetComponent<VideoPlayer>();
      12.         videoPlayer.Pause();
      13.         videoPlayer2.Pause();
      14.  
      15.     }
      16.     void Start()
      17.     {
      18.       timeUntilNextVideo = 0f;
      19.  
      20.     }
      21.     void Update()
      22.     {
      23.    if (Time.time > timeUntilNextVideo)
      24.        {
      25.            if(assignedPlayer==false){
      26.              videoPlayer.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)]; }
      27.            else if(assignedPlayer==true){
      28.              videoPlayer2.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)]; }
      29.             timeUntilNextVideo = Time.time + (float)videoPlayer.clip.length-0.1f;
      30. if(assignedPlayer==false){
      31.              videoPlayer.Play();
      32.                 assignedPlayer=true;
      33.         }
      34. else  if(assignedPlayer==true){
      35.              videoPlayer2.Play();
      36.                 assignedPlayer=false;
      37.         }
      38.  
      39. }
      40.     }
      41. }
      42.  
     
  11. ladyofla

    ladyofla

    Joined:
    Oct 19, 2015
    Posts:
    7
    Hi richardzzzarnold,
    Beautiful code. I'm new to unity and am trying to do exactly what you describe in the above code. Might you please tell me how I can make this work with a few clips? Where do I place the clips and how do I make the code start running? To what do I attach the code? It might be simpler if you could send me a stripped-down project with just a few clips in it to get me started. Thanks,
     
unityunity