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?
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?
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();
}
}
@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+
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;
}
}
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;
}
}
}
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;
}
I just recently made a script that handles this all for you, perfect for playing short intro segments before looping background music…
I’ve uploaded the scripts to Google Drive, just click here. :3
You can also use async along with foreach to run through your audio clips with appropriate delay. Example code here
This answer provided by Unity removed the small delay (1-3 frames) for me when playing the next clip. You need to ‘warm up’ the AudioSource ahead of time and schedule the next clip to play at the right moment.
Open your audio player software or programming environment.
Load the first audio file into the player and start playing it.
Register an event listener or a callback function that listens for the “end of file” event or completion of the playback of the first audio file.
Once the “end of file” event is detected, load the second audio file into the player.
Start playing the second audio file once it is loaded.