/// <summary> /// Plays an AudioEvent. /// </summary> /// <param name="audioEvent">The AudioEvent to play.</param> /// <param name="emitter">The GameObject on which the AudioEvent is to be played.</param> /// <param name="primarySource">The AudioSource component to use as the primary source for the event.</param> /// <param name="secondarySource">The AudioSource component to use as the secondary source for the event.</param> /// <param name="messageOnAudioEnd">The Message to Send to the GameObject when the sound has finished playing.</param> private void PlayEvent(AudioEvent audioEvent, GameObject emitter, AudioSource primarySource, AudioSource secondarySource, string messageOnAudioEnd = null) { ActiveEvent tempEvent = ActiveEventsGameObject.AddComponent <ActiveEvent>(); tempEvent.Initialize(audioEvent, emitter, primarySource, secondarySource); activeEvents.Add(tempEvent); // The base class owns this event once we pass it to PlayContainer, and may dispose it if it cannot be played. Action onPlayCompletedCallback = null; onPlayCompletedCallback = () => { RemoveEventInstance(tempEvent); tempEvent.OnPlayCompleted -= onPlayCompletedCallback; // Send message notifying user that sound is complete if (!string.IsNullOrEmpty(messageOnAudioEnd)) { tempEvent.AudioEmitter.SendMessage(messageOnAudioEnd); } }; tempEvent.OnPlayCompleted += onPlayCompletedCallback; tempEvent.StartEvent(); }
/// <summary> /// Play a single MiniAudioEvent on the predetermined source(s) /// </summary> /// <param name="audioEvent">The MiniAudioEvent to play</param> private void PlayEvent(MiniAudioEvent audioEvent) { if (audioEvent.primarySource == null) { Debug.LogErrorFormat(this, "Emitter on object \"{0}\" is null! Cannot play sound.", audioEvent.name); return; } if (audioEvent.container.IsContinuous) { if (audioEvent.secondarySource == null) { Debug.LogErrorFormat(this, "Secondary emitter on event \"{0}\" is null! Cannot play continuous sound.", audioEvent.name); } } ActiveEvent tempEvent = ActiveEventsGameObject.AddComponent <ActiveEvent>(); tempEvent.Initialize(audioEvent, audioEvent.primarySource.gameObject); // Do this last. The base class owns this event once we pass it to PlayContainer, and may dispose it if it cannot be played. StartCoroutine(audioEvent.container.PlayAsync(tempEvent)); }