- Home /
How to play an audio file after another finishes
Hello!
I'd want my level to start with a sound (start engine) and then after that sound is finished start the constant engine loop. How do I code that?
Answer by Tuncer · Feb 19, 2015 at 11:40 AM
You need only one AudioSource. Add this script and assign AudioClips.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
public AudioClip engineStartClip;
public AudioClip engineLoopClip;
void Start()
{
GetComponent<AudioSource> ().loop = true;
StartCoroutine(playEngineSound());
}
IEnumerator playEngineSound()
{
audio.clip = engineStartClip;
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = engineLoopClip;
audio.Play();
}
}
It works, kind of. It does however play the engine start -sound 1,5 times before starting the engine loop. Why do you think that happens?
$$anonymous$$aybe, your engineStart audio clip has empty noise at the end, you need to cut it.
I don't think it has (checked with audacity). Any way to make sure?
Did you get to fix this? I also tried kbaloch's solution and I get a small waiting time between the first and the second clip. (No empty noise anywhere in the samples)
Answer by dcarneiro · Aug 24, 2018 at 03:04 PM
@HonoraryBob I tried what you guys told but it still have a pause/delay. But this post helped me to find a solution!
This worked perfectly for me:
musicIntro.Play ();
musicLoop.PlayDelayed(musicIntro.clip.length);
Hope it helps someone in 2018+ :D
Do I put this code in the Start() function of the script or in a CoRoutine function?
It is something that should get called on the start, like in the Start() function. Coroutines are for continuous loops. Play() and playdelayed() are just 2 commands that exit after being called. Unity itself handles all the background checking.
Answer by Farwall · Dec 28, 2017 at 04:37 AM
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class LoopBGM : MonoBehaviour
{
public AudioClip StartClip;
public AudioClip LoopClip;
void Start()
{
StartCoroutine(playSound());
}
IEnumerator playSound()
{
GetComponent<AudioSource>().clip = StartClip;
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(StartClip.length);
GetComponent<AudioSource>().clip = LoopClip;
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = true;
}
}
how would I change this so I can play three clips instead of two?
Answer by hacky97 · Jan 07, 2017 at 08:26 PM
The problem is that switching the audioclip adds a small delay to the processing. It is also a good idea to give unity some time to preload everything.
add 2 audiosources. Wait for the first one to finish and play the second one.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class QueueAudioClip: MonoBehaviour
{
public AudioSource audioSourceIntro;
public AudioSource audioSourceLoop;
private bool startedLoop;
void FixedUpdate()
{
if (!audioSourceIntro.isPlaying && !startedLoop)
{
audioSourceLoop.Play();
Debug.Log("Done playing");
startedLoop = true;
}
}
}
It is the only way to avoid delays due to loading of the clip. Unity provides no convenient way to detect the end of a clip and also it might take a while before a clip starts playing. Hence: preload both clips by using 2 audiosources and keep an eye on the isPlaying property.
Answer by kbaloch · Feb 19, 2015 at 11:42 AM
you should do something like this
AudioClip otherClip;
bool playNow = false;
yield WaitForSeconds (audio.clip.length);
playNow = true;
void Update () {
if(playNow) {
// Assign the other clip
audio.clip = otherClip;
audio.Play();
playNow = false;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Car Audio c# script not working 3 Answers