- Home /
Audio: how do I immediately play a new audioclip without delay(code included)?
I currently have 2 Background music audio clips which I want to play immediately one after another (intro followed by a loop when the intro is done playing).
However, no matter what I try, there seems to me a noticeable gap when transitioning from one track to an other... Is there any way around this (other than crossfading)
Below is what I have come up with so far:::
///////////////////////////////////////////////////////////
private static bool loopStarted = false;
public AudioClip backGroundMusic_Intro;
public AudioClip backGroundMusic_Loop;
//--Separate audio sources to try and avoid loading delays
private AudioSource bgA;
private AudioSource bgB;
void Awake(){
bgA = gameObject.AddComponent<AudioSource>();
bgB = gameObject.AddComponent<AudioSource>();
bgA.clip = backGroundMusic_Intro;
bgB.clip = backGroundMusic_Loop;
bgA.loop = false;
bgB.loop = true;
bgA.Play ();
}
// Fixed Update called multiple times per frame
void FixedUpdate () {
if(loopStarted == false && !(bgA.isPlaying)){
bgB.Play();
Destroy (bgA);
loopStarted = true;
}
}
///////////////////////////////////////////////////////////
I'm a noob but thought I would throw my 2 cents in. You may have to use a different type of script though. Have your 2nd audio clip set up on "yeild waitforseconds() and put in exactly how many seconds your first one is?? so the 2nd clip would start playing right after the first one?? Add play one shot on first clip and loop on 2nd?? Not sure if that would work but you could try somthing like that??
Answer by JoeStrout · Jun 26, 2016 at 07:36 PM
It is your script, actually. You can't use Play to stitch together audio clips seamlessly like this. You need to instead use PlayScheduled.
Answer by 5argon · Oct 29, 2017 at 12:43 PM
I have build a solution called Introloop (http://exceed7.com/introloop/). It uses various Scheduled methods together with multiple AudioSources to achieve seamless transition from intro to looping part.
If you want to build it yourself, in that website I have also explained my approach. You can get the rough idea if you are new to audio scripting.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to have two audio listeners...splitscreen 4 Answers
Multiple Tiled GPU warning: RenderTexture color surface errors? 0 Answers
How to Name Individual Buttons Within a List 2 Answers
C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers