Playing multiple audio clips in unity

3.1k Views Asked by At

There might be similar questions to this one but I couldn't find any useful.

I have a simple scrips which has a list of classes that contains audio clips, id, and event system individually for each one of those elements.

I need to make that audio clip fields to list and make them play by order until the last one then stop and fire event.

Here's my code for only one audio clip slot: (Also i tried audio clip array but its just playing the first one on the audio clip array)

public class MainAudioManager : MonoBehaviour {


public List<soundInfo> currentsoundinfoList = new List<soundInfo>();

AudioSource audioSource;

soundInfo curentSoundInfo;

float audioLenght;


void Start () {


    audioSource = gameObject.GetComponent<AudioSource> ();

}


public void SetAudioToPlay (int ID) {


    for (int i = 0; i < currentsoundinfoList.Count; i++) {

        if (currentsoundinfoList [i].ID == ID) {

            curentSoundInfo = currentsoundinfoList[i];

            audioSource.clip = curentSoundInfo.clipToPlay;

            audioSource.Play ();

            audioLenght = audioSource.clip.length;

            StartCoroutine ("waitnigTillEbd");


            return;

        }

    }


}


IEnumerator waitnigTillEbd () {



    yield return new WaitForSeconds (audioLenght + 1f);

    curentSoundInfo.eventOnSound.Invoke ();

}


[System.Serializable]

public class soundInfo {


    public string name;

    public int ID;

    public AudioClip clipToPlay;

    public UnityEvent eventOnSound;

}

}

1

There are 1 best solutions below

0
On

alright i found the solution by my self. here is the final result for creating list of sound with event onstop attached to each one of them individually and also playable by ID from another event and classes ,for anyone who need it

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Events;


[RequireComponent(typeof(AudioSource))]

public class MainAudioManager : MonoBehaviour {


public List<soundInfo> soundList = new List<soundInfo>();

AudioSource audioSource;

soundInfo curentSoundInfo;

float audioLenght;


void Start () {


    audioSource = gameObject.GetComponent<AudioSource> ();

}


public void SetAudioToPlay (int ID) {



        for (int i = 0; i < soundList.Count; i++) {


            if (soundList [i].ID == ID) {

                curentSoundInfo = soundList [i];

                StartCoroutine(playSequencely());

                return;

        }

    }

}

  IEnumerator playSequencely () {


    yield return null;


    for (int cnt = 0; cnt < curentSoundInfo.clipsToPlay.Length; cnt++) {


        audioSource.clip = curentSoundInfo.clipsToPlay [cnt];

        audioSource.Play ();


        while (audioSource.isPlaying) {


            yield return null;

        }


        }

    //Debug.Log ("Last Audio Is Playing");

    curentSoundInfo.onStop.Invoke ();

    }

} 


[System.Serializable]

public class soundInfo {


    public string name;

    public int ID;

    [TextArea (2, 8)] public string About;

    public AudioClip[] clipsToPlay;

    //public float delayBetween;

    public UnityEvent onStop;

}