How to Play sequentially more than one AudioSource in one game object ?
I have 3 AudioSource component in main camera. There was : audio1.mp3 , audio2.mp3, audio3.mp3. And i want to play it sequentially the first i want to play audio1.mp3, after it end then it'll automatically play audio2.mp3, and so on. I don't have any idea about this.
Answer by TBruce · Jul 17, 2016 at 03:20 AM
Here is a class that will allow you to do just that. It has two public lists, one is of the AudioClips and the other is for the AudioSources.
The class is flexible in that the only main requirement is that you have to set the AudioClip list soundList . This can be set to as many AudioClips as you want, it is not limited to three.
This class could also be modified to use just one AudioSource. Is there any reason why you need more than one?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AudioManager : MonoBehaviour
{
[Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
public List<AudioClip> soundList = new List<AudioClip>();
[Tooltip("List of AudioSource components. This list does not need to be set.")]
public List<AudioSource> audioSourceList = new List<AudioSource>();
[Tooltip("This variriable resets the audioSourceList and creates a separate AudioSource for each clip if set to true.")]
public bool restAudioSourceList = false;
[Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
public bool playOnAwake = false;
[Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
public bool loop = false;
private bool soundsGoodToPlay = false;
void Start ()
{
if (soundList.Count > 0)
{
// this loop just make sure that there are valid AudioClips in the soundList
for (int i = (soundList.Count - 1); i >= 0; i--)
{
if (soundList[i] == null)
{
soundList.RemoveAt(i);
}
}
if (soundList.Count > 0)
{
if ((restAudioSourceList) || (audioSourceList.Count == 0) || (audioSourceList.Count < soundList.Count))
{
audioSourceList.Clear();
for (int i = 0; i < soundList.Count; i++)
{
AudioSource source = gameObject.AddComponent<AudioSource>();
source.playOnAwake = playOnAwake;
source.loop = loop;
audioSourceList.Add(source);
}
}
soundsGoodToPlay = (soundList.Count > 0);
}
}
}
void PlaySequentialSounds()
{
StartCoroutine(PlaySounds());
}
IEnumerator PlaySounds()
{
if (soundList.Count > 0)
{
for (int i = 0; i < soundList.Count; i++)
{
audioSourceList[i].PlayOneShot(soundList[i]);
// wait for the lenght of the clip to finish playing
yield return new WaitForSeconds(soundList[i].length);
}
}
yield return null;
}
}
The following class does the same as the one above but uses only one AudioSource
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AudioManager : MonoBehaviour
{
[Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
public List<AudioClip> soundList = new List<AudioClip>();
[Tooltip("The AudioSource component to play the AudioClips.")]
public AudioSource audioSource;
[Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
public bool playOnAwake = false;
[Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
public bool loop = false;
private bool soundsGoodToPlay = false;
void Start ()
{
// make sure we have a reference to the AudioSource
if (audioSource == null)
{
if (audioSource == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
else
{
audioSource = gameObject.GetComponent<AudioSource>();
}
}
audioSource.playOnAwake = playOnAwake;
audioSource.loop = loop;
// this loop just make sure that alll the AudioClips in the soundList are valid
for (int i = (soundList.Count - 1); i >= 0; i--)
{
if (soundList[i] == null)
{
soundList.RemoveAt(i);
}
}
}
void PlaySequentialSounds()
{
StartCoroutine(PlaySounds());
}
IEnumerator PlaySounds()
{
if (soundList.Count > 0)
{
for (int i = 0; i < soundList.Count; i++)
{
audioSource.PlayOneShot(soundList[i]);
// wait for the lenght of the clip to finish playing
yield return new WaitForSeconds(soundList[i].length);
}
}
yield return null;
}
}
Thanks. :D Because i want put my command voice in my game, and i divide it into several part. But, when i put it all those things into main camera, its play on the same time. The only way i know to put audio in my game is by put it in main camera. If you have better method, it'll be my pleasure to know it. Thank you very much.
You can have an AudioSource anywhere, it does not have to be on the $$anonymous$$ainCamera. Because you can only have one AudioListener per scene is the reason why the AudioListener is on the main camera.
You can attach either of the classes above to a separate GameObject. I usually have a GameObject called "Game $$anonymous$$anager" in my game where I place all my game management components (or I make child GameObjects) "e.g. Game Object $$anonymous$$anager, Audio $$anonymous$$anager..."