Search Unity

Question Multiple video sources for different segments of a song (¿Ideas?)

Discussion in 'Scripting' started by joacoerazo, Aug 20, 2020.

  1. joacoerazo

    joacoerazo

    Joined:
    Dec 6, 2014
    Posts:
    4
    Hi to all

    I have a song divided in 17 segments, each segment has 2 or 3 possible videos (I need to change them randomly in that segment). Any clue on how to do it?
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    How have you segmented the song, how are you playing it back? Show us some code and we might be able to help.
     
  3. joacoerazo

    joacoerazo

    Joined:
    Dec 6, 2014
    Posts:
    4
    This one works with a single segment, it doesn't work with multiple pieces.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5. public class Player1 : MonoBehaviour
    6. {
    7.     public VideoClip[] VideoClipArray;
    8.     private VideoPlayer videoPlayer;
    9.     private float timeUntilNextVideo;
    10.     void Awake()
    11.     {
    12.         videoPlayer = GetComponent<VideoPlayer>();
    13.         videoPlayer.Pause();
    14.     }
    15.     void Start()
    16.     {
    17.         timeUntilNextVideo = 0f;
    18.     }
    19.     void Update()
    20.     {
    21.         if (Time.time > timeUntilNextVideo)
    22.         {
    23.             videoPlayer.clip = VideoClipArray[Random.Range(0, VideoClipArray.Length)];
    24.             timeUntilNextVideo = Time.time + (float)videoPlayer.clip.length;
    25.             videoPlayer.Play();
    26.         }
    27.     }
    28. }
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Sounds like a cool project!
    You probably want an array for each segment that holds the 2 or 3 possible clips. That would be 17 arrays, which sounds like a pain, but it's the most flexible (each segment can have a different number of possible clips). Then you can set the next video using an int variable that tracks which segment is playing (that accesses the correct array), then use the same Random.Range(0, VideoClipArray.Length)....except this array is one of the 17 and has 2 or 3 possibilities in it.
    Hope that makes sense.
     
    Munchy2007 likes this.
  5. joacoerazo

    joacoerazo

    Joined:
    Dec 6, 2014
    Posts:
    4
    Hi seejayjames,

    I have tried but it does´t work. it keep playing the first array.
    How do I make the transition from one array to another?