Hello
I have 2 version of a music loop so I am using the audio mixer to blend them depending on where someone is in a level, however there is a beginning to the song that I want to play before the looping music plays.
Is there a way to pause the looping music until the intro music is finished?
I managed to figure it out so I will post my solution until a better one is found.
Basically
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
public class ClassName: MonoBehaviour {
public AudioSource introMusic;
public GameObject loopMusicGourp;
private AudioSource[] loopMusic;
[System.Serializable]
private class MusicProperties
{
public AudioMixerSnapshot snapShot;
public float timeStamp;
public float fadeInTime;
private bool hasPlayed;
public bool HasPlayed {
get {return hasPlayed; }
set { hasPlayed = value; }
}
}
void Awake () {
loopMusic = loopMusicGourp.GetComponents<AudioSource> ();
}
public IEnumerator Start() {
introMusic.Play ();
while (introMusic.isPlaying)
yield return new WaitForEndOfFrame();
foreach (AudioSource song in loopMusic)
song.Play ();
}
void Update () {
foreach (MusicProperties m in musicProperties) {
if (yourTimer > m.timeStamp && !m.HasPlayed) {
m.snapShot.TransitionTo m.fadeInTime);
m.HasPlayed = true;
}
}
}
}
Hope this helps someone out there.
@ RandomDood thanks so much for sharing! This is very helpful