Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question audio music player with time delay

Discussion in 'Audio & Video' started by sinpros, May 22, 2021.

  1. sinpros

    sinpros

    Joined:
    Dec 23, 2012
    Posts:
    20
    im trying to create a music script that will play random music in a game scene, but i want like a 5 minute delay in between songs. this is my current code. there is no delay yet. the music just goes from one song to the next. but i want 5 minutes in between tracks. any help would be awesome


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MusicPlayer : MonoBehaviour {
    public AudioClip[] clips;
    private AudioSource audioSource;
    void Start () {
    audioSource = FindObjectOfType<AudioSource>();
    audioSource.loop = false;
    }

    private AudioClip GetRandomClip()
    {
    return clips[Random.Range(0,clips.Length)];
    }

    void Update () {
    if (!audioSource.isPlaying)
    {
    audioSource.clip = GetRandomClip();
    audioSource.Play();
    }
    }
    }
     
  2. sinpros

    sinpros

    Joined:
    Dec 23, 2012
    Posts:
    20
    anyone?
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,306
    If you don't need timing to be incredibly accurate (and it sounds like you don't) then you can do something as easy as this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicPlayer : MonoBehaviour
    6. {
    7.     public AudioClip[] clips;
    8.     private AudioSource audioSource;
    9.  
    10.     //5 second delay for testing
    11.     public float DelayTargetTime = 5f;
    12.     //public only so that you can watch it count up in the inspector
    13.     public float DelayTimer = 0f;
    14.  
    15.     //A flag we can use to turn off audio scheduling if we need to
    16.     public bool PlaylistSchedulingEnabled = true;
    17.  
    18.     void Start()
    19.     {
    20.         //If your audiosource component is attached to the same gameobject as this script
    21.         //you should use audioSource = gameObject.GetComponent<AudioSource>();
    22.         //not audioSource = FindObjectOfType<AudioSource>();
    23.         audioSource = gameObject.GetComponent<AudioSource>();
    24.         audioSource.loop = false;
    25.         //we will play the first clip immediately by setting the delay timer to our target delay
    26.         DelayTimer = DelayTargetTime;
    27.     }
    28.  
    29.     private AudioClip GetRandomClip()
    30.     {
    31.         return clips[Random.Range(0, clips.Length)];
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (!audioSource.isPlaying)
    37.         {
    38.             //has our target delay elapsed?
    39.             if (DelayTimer >= DelayTargetTime)
    40.             {
    41.                 if (PlaylistSchedulingEnabled)
    42.                 {
    43.                     //reset our timer
    44.                     DelayTimer = 0f;
    45.  
    46.                     audioSource.clip = GetRandomClip();
    47.                     audioSource.Play();
    48.                 }
    49.             }
    50.             else
    51.             {
    52.                 //add the time that has passed to our delaytimer
    53.                 DelayTimer += Time.deltaTime;
    54.             }
    55.  
    56.         }
    57.     }
    58. }
    59.  

    use code tags next time you post code
    https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Last edited: May 29, 2021
  4. JoshuaGrice

    JoshuaGrice

    Joined:
    May 31, 2021
    Posts:
    2
    As you said, you are trying to make a music script that will play random music in a game scene. But you need 5 minutes delay between songs. You can set a timer for that. Music will be delayed from one song to another with a delay of 5 minutes. As you needed.
     
  5. sinpros

    sinpros

    Joined:
    Dec 23, 2012
    Posts:
    20
    thank you guys for the help. i got it working