///////////////////////////////////////////////////////////////////////////// /// Function: StopSound ///////////////////////////////////////////////////////////////////////////// public static void StopSound(int iSourceID, bool bFadeOut = true) { // The below GameObject will contain the Audio Object. GameObject goAudioObject = null; // Loop through all active audio objects and retrieve the object that // matches the provided id. foreach (GameObject goAudio in m_liActiveAudioObjects) { if (goAudio == null) { m_liActiveAudioObjects.Remove(goAudio); return; } // Attempt to retrieve the audio clip object. CAudioClip aClip = goAudio.GetComponent <CAudioClip>(); if (aClip == null) { return; } if (aClip.ClipId == iSourceID) { // We found a match. goAudioObject = goAudio; } } // Check if we managed to find the gameobject. if (goAudioObject == null) { return; } // Retrieve the clip information object. CAudioClip cClipInfo = goAudioObject.GetComponent <CAudioClip>(); // Check if it's marked for deletion. if (true == cClipInfo.MarkedForDestruction) { // Take out the object and try stopping the sound again. m_liActiveAudioObjects.Remove(goAudioObject); StopSound(iSourceID, bFadeOut); // No point in going forward, we can exit the function. return; } else { // Take out the object and try stopping the sound again. m_liActiveAudioObjects.Remove(goAudioObject); // Ensure we don't reprocess this clip. cClipInfo.MarkedForDestruction = true; } // Retrieve the audio source for the fadeout functionality. AudioSource asSource = goAudioObject.GetComponent <AudioSource>(); if (true == bFadeOut) { CStaticCoroutine.DoCoroutine(FadeOut(asSource)); } else { asSource.volume = 0; } }
///////////////////////////////////////////////////////////////////////////// /// Function: CreateAndPlayAudio ///////////////////////////////////////////////////////////////////////////// public static int CreateAndPlayAudio(Vector3 v3Position, string strAudioName, bool bLoop, bool bPlayOnAwake, bool bFadeIn, float fVolume) { // This function will create a GameObject using the provided parameters and will add an // AudioSource component to it which we will configure to suit our needs. The GameObject // will be destroyed once we're done with it. string strFunctionName = "CAudioControl::CreateAndPlayAudio()"; // Check if we have a collection available for the provided audio name. if (false == m_dAudioClipContainer.ContainsKey(strAudioName)) { Debug.LogError(string.Format("{0} {1} " + ErrorStrings.ERROR_UNRECOGNIZED_NAME, strFunctionName, strAudioName)); } // Get a list of audio clips available to us. List <AudioClip> liAudioClips = m_dAudioClipContainer[strAudioName]; // Attempt to select a random clip from the list. AudioClip aClip = liAudioClips.OrderBy(x => Guid.NewGuid()).FirstOrDefault(); GameObject goAudioClipObject = new GameObject(strAudioName); goAudioClipObject.tag = Tags.TAG_AUDIO; // Set the position of the object. goAudioClipObject.transform.position = v3Position; // Add the Audio Source component goAudioClipObject.AddComponent <AudioSource>(); // Add the Clip Info object. goAudioClipObject.AddComponent <CAudioClip>(); // Retrieve the Audio source component. We will use this reference to set up values, etc. AudioSource asSource = goAudioClipObject.GetComponent <AudioSource>(); // Retrieve the Clip information object. CAudioClip cClipInfo = goAudioClipObject.GetComponent <CAudioClip>(); // Set up the audio source. asSource.playOnAwake = bPlayOnAwake; asSource.loop = bLoop; asSource.clip = aClip; asSource.volume = 0f; // Check if we want to fade in the sound. if (true == bFadeIn) { CStaticCoroutine.DoCoroutine(FadeIn(asSource, fVolume)); } else { asSource.volume = fVolume; } // Play the clip and destroy the game object once we're done with it. asSource.Play(); if (false == bLoop) { Destroy(goAudioClipObject, aClip.length); } else { m_liActiveAudioObjects.Add(goAudioClipObject); return(cClipInfo.ClipId); } // Return an invalid id. return(-1); }