public void Update() { t = 0; foreach (var sound in sounds) { AudioSource aSource = sound.Key; if (sound.Value.doesLoop) { SoundDefinition musicDefinition = sound.Value; t = (double)aSource.timeSamples / (double)aSource.clip.frequency; if (t >= musicDefinition.loopPoint) { aSource.Play(); aSource.timeSamples = (int)(musicDefinition.loopBackTo * ((double)aSource.clip.frequency)); } } else { if (aSource.time > aSource.clip.length) { Stop(aSource); } } } }
public static bool IsPlaying(SoundDefinition sound) { foreach (AudioSource audio in sounds.Keys) { if (sounds[audio] == sound) { return(true); } } return(false); }
public static GameObject Play(SoundDefinition sound, float startTime = 0, Transform parent = null) { if (!sound) { return(null); } GameObject go = new GameObject(sound.name); go.transform.SetParent(parent); AudioSource aSource = go.AddComponent <AudioSource>(); aSource.clip = sound.sound; aSource.volume = sound.volume; aSource.loop = sound.doesLoop; aSource.outputAudioMixerGroup = sound.mixerGroup; aSource.time = startTime == 0 ? sound.startTime : startTime; aSource.Play(); sounds.Add(aSource, sound); return(go); }