/// <summary> /// Removes the specified AudioStackItem from the AudioStack. /// </summary> /// <param name="item">An AudioStackItem.</param> public void Remove(AudioStackItem item) { if (!IsLoaded(item)) { return; } // Make sure the item is stopped item.Source.Stop(); // Remove the clip association item.Source.clip = null; // Add the source back to the stack _audioSources.Push(item.Source); // Remove our item definition _loadedItems.Remove(item.Key); // Free Item item = null; }
/// <summary> /// Determines whether an AudioStackItem is playing. /// </summary> /// <returns>Is the AudioStackItem playing?</returns> /// <param name="item">An AudioStackItem.</param> public bool IsPlaying(AudioStackItem item) { return(item != null && item.Source != null && item.Source.isPlaying); }
/// <summary> /// Add/Load an AudioStackItem /// </summary> /// <param name="item">An AudioStackItem.</param> /// <param name="createDuplicate">Create a duplicate entry if need be.</param> public string Add(AudioStackItem item, bool createDuplicate) { if (IsLoaded(item.Key) && !createDuplicate) { // Update any settings we need too _loadedItems [item.Key].Fade = item.Fade; _loadedItems [item.Key].Loop = item.Loop; _loadedItems [item.Key].PlayOnLoad = item.PlayOnLoad; _loadedItems [item.Key].FadeInSpeed = item.FadeInSpeed; _loadedItems [item.Key].FadeOutSpeed = item.FadeOutSpeed; _loadedItems [item.Key].Persistant = item.Persistant; _loadedItems [item.Key].TargetVolume = item.TargetVolume; _loadedItems [item.Key].StartVolume = item.StartVolume; _loadedItems [item.Key].RemoveAfterFadeOut = item.RemoveAfterFadeOut; _loadedItems [item.Key].Priority = item.Priority; if (_loadedItems [item.Key].Source != null) { _loadedItems [item.Key].Source.priority = item.Priority; } } else { // If we do not have enough sources, we should make one, if we can. if (_audioSources.Count <= 0 && _loadedItems.Count < MaximumSources) { var source = _poolObject.AddComponent <AudioSource> (); // Make sure we dont have any fun little hickups source.playOnAwake = false; _audioSources.Push(source); } // Let's rock this playing of one if (_audioSources.Count > 0) { // Create a slightly different key if need be // You should never be starting the exact same sound twice in the same frame, // thus we can just use the this simple addition if (createDuplicate && IsLoaded(item.Key)) { item.Key = item.Key + Time.time + item.GetHashCode(); } // Use this stack for callback // TODO: Don't like this item.Stack = this; item.Source = _audioSources.Pop() as AudioSource; // Update Our Source item.Source.clip = item.Clip; item.Source.volume = item.StartVolume; item.Source.loop = item.Loop; item.Source.priority = item.Priority; // Auto Play Stuff if (item.PlayOnLoad) { item.Source.Play(); } _loadedItems.Add(item.Key, item); } else if (_audioSources.Count <= 0 && UsePriorities) { // Find our sucker to replace. //TODO: Maybe we can search for how long a track has already played AudioStackItem replaceItem = item; foreach (string s in _loadedItems.Keys.ToList()) { if (_loadedItems [s].Priority < replaceItem.Priority && !_loadedItems [s].Loop) { replaceItem = _loadedItems [s]; } } // Did any thing qualify? if (replaceItem != item) { Remove(replaceItem); Add(item); } else { Debug.Log("No available Audio Sources from the AudioStack to use, even when prioritized."); } } else { Debug.Log("No available Audio Sources from the AudioStack to use."); } } return(item.Key); }
/// <summary> /// Determines whether an AudioStackItem is currently loaded and managed by the AudioStack. /// </summary> /// <returns>Is the AudioStackItem present in the AudioStack?</returns> /// <param name="item">An AudioStackItem.</param> public bool IsLoaded(AudioStackItem item) { return(item != null && IsLoaded(item.Key)); }
/// <summary> /// Add/Load an AudioStackItem /// </summary> /// <param name="item">An AudioStackItem.</param> public string Add(AudioStackItem item) { return(Add(item, false)); }