Run Bpf Again Once Another Finishes
- Spaces
- Default
- Assist Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
9
How to play an audio file after some other finishes
Hullo!
I'd want my level to starting time with a audio (start engine) and and then after that audio is finished start the constant engine loop. How do I code that?
7 Replies
· Add together your reply
- Sort:
xi
Answer by Tuncer · February 19, 2015 at 11:twoscore 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.prune = engineStartClip; audio.Play(); yield return new WaitForSeconds(audio.clip.length); audio.clip = engineLoopClip; audio.Play(); } }
viii
Respond by dcarneiro · Aug 24, 2018 at 03:04 PM
@HonoraryBob I tried what you guys told simply it still have a pause/delay. Just this post helped me to find a solution!
This worked perfectly for me:
musicIntro.Play (); musicLoop.PlayDelayed(musicIntro.prune.length);
Hope it helps someone in 2018+ :D
And I used what @hacky97 said: 2 AudioSources!
This is even more beautiful. Cool.
Practise I put this code in the Start() function of the script or in a CoRoutine office?
It is something that should get called on the start, like in the Start() office. Coroutines are for continuous loops. Play() and playdelayed() are just 2 commands that go out later on being called. Unity itself handles all the background checking.
Perfect! Sounds actually good. Cheers! Information technology helps in 2020 ;)
3
Answer by Farwall · Dec 28, 2017 at 04:37 AM
using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] public grade LoopBGM : MonoBehaviour { public AudioClip StartClip; public AudioClip LoopClip; void Start() { StartCoroutine(playSound()); } IEnumerator playSound() { GetComponent<AudioSource>().prune = StartClip; GetComponent<AudioSource>().Play(); yield return new WaitForSeconds(StartClip.length); GetComponent<AudioSource>().prune = LoopClip; GetComponent<AudioSource>().Play(); GetComponent<AudioSource>().loop = truthful; } }
how would I change this and so I tin can play iii clips instead of two?
2
Answer past hacky97 · January 07, 2017 at 08:26 PM
The problem is that switching the audioclip adds a small filibuster to the processing. It is besides a good idea to give unity some time to preload everything.
add 2 audiosources. Wait for the first i to finish and play the second ane.
using UnityEngine; using Organisation.Collections; [RequireComponent(typeof(AudioSource))] public class QueueAudioClip: MonoBehaviour { public AudioSource audioSourceIntro; public AudioSource audioSourceLoop; individual bool startedLoop; void FixedUpdate() { if (!audioSourceIntro.isPlaying && !startedLoop) { audioSourceLoop.Play(); Debug.Log("Done playing"); startedLoop = true; } } }
@hacky97 Is that the just manner to avert a delay?
It is the only way to avoid delays due to loading of the prune. Unity provides no convenient way to detect the end of a prune and also it might take a while before a clip starts playing. Hence: preload both clips past using 2 audiosources and keep an center on the isPlaying property.
0
Answer by kbaloch · Feb xix, 2015 at xi:42 AM
you should do something like this
AudioClip otherClip; bool playNow = faux; yield WaitForSeconds (audio.clip.length); playNow = true; void Update () { if(playNow) { // Assign the other clip audio.clip = otherClip; sound.Play(); playNow = faux; }
- one
- 2
- ›
Your answer
Welcome to Unity Answers
The all-time identify to inquire and answer questions about development with Unity.
To aid users navigate the site we have posted a site navigation guide.
If yous are a new user to Unity Answers, check out our FAQ for more information.
Brand certain to cheque out our Knowledge Base for normally asked Unity questions.
If you are a moderator, see our Moderator Guidelines folio.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/904981/how-to-play-an-audio-file-after-another-finishes.html
Information technology works, kind of. It does however play the engine showtime -sound ane,5 times earlier starting the engine loop. Why practice you lot remember that happens?
$$anonymous$$aybe, your engineStart audio clip has empty noise at the end, yous demand to cut it.
I don't think it has (checked with audacity). Any way to brand sure?
Did you get to set this? I also tried kbaloch'southward solution and I become a small waiting fourth dimension between the start and the 2nd clip. (No empty noise anywhere in the samples)
TYS$$anonymous$$!!! I needed this! :D