Loop seemlessly on several AudioSource

hello guys !

I would like to add some music to my game. I have a game object with several AudioSource, and I don’t know how to loop on everyone…

I have a variable of type List of AudioSource, I could play the first one, and then use PlayQueued to chain them, but when my last music has played, it is over, and I want to start over with the first one.

An ugly solution would be to loop like a hundred times on PlayQueued to have some margin, but I’m looking for a better solution …

Does anyone has an idea ?

EDIT : here is the code I made :

private var listOfZiks:List.<AudioSource> = new List.<AudioSource>();
private var currMusic:AudioSource;
private var timer:float = 0.0f;
private var counter:int = 0;
private var hasToStop:boolean = false;

function Awake () {
	DontDestroyOnLoad(gameObject);
	
	introMusic = GetComponent(AudioSource);
	FindAllMusics();
}

function FindAllMusics() {
	var musicFolder:DirectoryInfo = new DirectoryInfo(Application.dataPath + "/Music");
	Debug.Log(Application.dataPath + "/Music/");
	if (musicFolder.Exists)
	{
		Debug.Log("Music folder exists");
		var musicFiles:FileInfo[] = musicFolder.GetFiles("*.wav");
		
		for (var i:int=0; i<musicFiles.Length; i++)
		{
			Debug.Log(musicFiles*.FullName);*
  •  	var currAudiosource:AudioSource = gameObject.AddComponent(AudioSource);*
    
  •  	currAudiosource.playOnAwake = false;*
    
  •  	// The data must be an audio clip in Ogg(Web/Standalones), MP3(phones), WAV, XM, IT, MOD or S3M format.*
    

_ var currWWW:WWW = new WWW(“file:///” + musicFiles*.FullName);_
_
currAudiosource.clip = currWWW.GetAudioClip(false, true, AudioType.WAV);_
_
listOfZiks.Add(currAudiosource);_
_
}_
_
}_
_
else Debug.Log(“Music folder does NOT exist :(”);_
_
}*_

function PlayPersonalMusic() {
* introMusic.Stop();*
* if (listOfZiks.Count <= 0) return;*

* LoopOnZiks();*
}

function LoopOnZiks() {
* while (!hasToStop)*
* { *
* currMusic = listOfZiks[counter];*
* timer = currMusic.clip.length;*
* Debug.Log("--------------- timer = " + timer);*
* currMusic.Play();*
* yield WaitForSeconds(timer);*
* ++counter;*
* if (counter == listOfZiks.Count) counter = 0;*
* }*
* hasToStop = false;*
}
My problem is now that the list listOfZiks seems correct, but I can see in the inspector that the clip is not valuated. And the weird thing : all the musics contained in the list are played all together once, and then they are played one by one correctly.
So I don’t really mind if the clips are not valuated (don’t understand why, but my list contains what I want apparently …), my real problem is the “play all songs” thing at the beginning …
Is there something huge I don’t see ?
NB : I tested with 2 songs, and I can see in the logs that I have 2 lines with timer = 0, so I tried to play and yield only if timer > 0 , and I got an infinite loop …

I would create a Coroutine that checks every 0.5 seconds the audio has finished playing Unity - Scripting API: AudioSource.isPlaying

public AudioClip[] m_clips;
public AudioSource m_as = 0;
private int m_currentClip;

public Start() {
    StartCoroutine(loopAudio());
}

private IEnumerator loopAudio() {
    while(true) {
        m_as.Stop();
        m_as.clip = m_clips[m_currentClip];
        m_as.Play();
        yield return new WaitForSeconds(m_clips[m_currentClip].length);
        m_currentClip++;
        m_currentClip % m_clips.Length;
    }
}

I’ve modified it to make it more efficient in terms of calling the coroutine,

Hope it helps.

try this javascript-

#pragma strict

 

var RandomStart : boolean = false;

var SongList : AudioClip[];

 

private var SongNumber : int = 0;

private var Music : AudioSource;

private var timer : float;

private var play : boolean = true;

 

function Start()

{

    gameObject.AddComponent(AudioSource);

 

    if(RandomStart)

    {

        SongNumber = Random.Range(0,SongList.Length);

    }

    

    Music = gameObject.GetComponent(AudioSource);

    Music.loop = false;

    Music.playOnAwake = true;

    Music.clip = SongList[SongNumber];

    Music.volume = 0.3;

    audio.Play();

}

 

function Update()

{

    timer = timer + Time.deltaTime;

    

    if(timer > (audio.clip.length + 1) && play == true)

    {

        play = false;

        if(SongNumber < (SongList.Length-1))

        {

            SongNumber = SongNumber + 1;

        }

        else

        {

            SongNumber = 0;

        }

        

        Music.clip = SongList[SongNumber];

        

        audio.Play();

        timer = 0;

        play = true;

    }

}

accept as solution if you this is what you are looking for.