// Switch the background music. public void SwitchBGM(AudioClip clip) { // Don't do anything if it is the same audioclip if (clip == bgmSource.clip) { return; } // Save bgm state before loading new sound SoundPlayerState originalBGMState = bgmState; // Stop background music if it is currently playing or paused before switching bgm if (bgmState != SoundPlayerState.Stopped) { StopBGM(); } // Set bgmSource clip to the new clip bgmSource.clip = clip; // Restore bgm state if (originalBGMState == SoundPlayerState.Paused) { PauseBGM(); } else if (originalBGMState == SoundPlayerState.Playing) { PlayBGM(); } }
// Stops all sounds effects currently playing (if any). // Sets the sfx state to stopped. // Note: this will also result in all stopped sfx being removed. public void StopAllSFX() { for (int i = 0; i < sfxSources.Count; i++) { StopSFX((AudioSourceManager)sfxSources[i]); } sfxState = SoundPlayerState.Stopped; }
// Pause the background music only if it is currently playing. public void PauseBGM() { // If we are playing something then pause it! if (bgmState == SoundPlayerState.Playing) { bgmSource.Pause(); } // No matter what, our state is now paused. bgmState = SoundPlayerState.Paused; }
// Play the background music if it isn't playing already. public void PlayBGM(bool loop = true, float pan = 0, ulong delay = 0) { // If we aren't already playing something then start the background music. if (bgmState != SoundPlayerState.Playing) { bgmSource.loop = loop; bgmSource.panStereo = pan; bgmSource.Play(delay); bgmState = SoundPlayerState.Playing; } }
// Pauses all sound effects currently playing (if any). // Sets the sfx state to paused. // Note: this will queue up paused sound effects when they are added to the sound manager (e.g., PlaySFX()). // These won't play until PlayAllSFX() is called. public void PauseAllSFX() { if (sfxState != SoundPlayerState.Paused) { for (int i = 0; i < sfxSources.Count; i++) { PauseSFX((AudioSourceManager)sfxSources[i]); } sfxState = SoundPlayerState.Paused; } }
// Play all sound effects that were paused (if any). // Sets the sfx state to playing. public void PlayAllSFX() { if (sfxState != SoundPlayerState.Playing) { for (int i = 0; i < sfxSources.Count; i++) { ((AudioSourceManager)sfxSources[i]).Play(); } sfxState = SoundPlayerState.Playing; } }
IEnumerator shiftMusic() { state = SoundPlayerState.SHIFTING; doWeNeedToplayTheSameSong = Random.Range(0, 100) < PropertiesSingleton.instance.soundProperties.musicCycleChanceInPercent; if (doWeNeedToplayTheSameSong) { yield return(StartCoroutine(playTheSameSong())); } else { yield return(StartCoroutine(StartDifferentSong())); } state = SoundPlayerState.NORMAL; }
// Used to play single sound clips. public AudioSource PlaySFX(AudioClip clip, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0, GameObject target = null, float vol = 1) { // Create a new sfx. AudioSource sfx = createAudioSource(clip, sfxVolume, loop, positionOffset, soundEventCallback, pan, target, vol); // Play the clip if we are in sfx playing or stopped state. // Change sfx state to playing if it was stopped before if (sfxState != SoundPlayerState.Paused) { sfx.Play(delay); sfxState = SoundPlayerState.Playing; } // Return a reference to the AudioSource for later use. return(sfx); }
void Start() { // Initialiaze our array of sound effects. sfxSources = new ArrayList(); // Set our soundplayerstates for sfx and bgm, and set bgm volume sfxState = SoundPlayerState.Playing; if (bgmSource.isPlaying) { bgmState = SoundPlayerState.Playing; } else { bgmState = SoundPlayerState.Stopped; } bgmSource.volume = bgmVolume; }
// Update our sfx array and remove any sound effects that are done playing. void Update() { // Iterate through our sound effects and see if they are done yet... Then remove them. // Don't do this if the sfxstate is paused since the sfx won't be playing. if (sfxState != SoundPlayerState.Paused) { for (int i = sfxSources.Count - 1; i >= 0; i--) { AudioSourceManager sfx = (AudioSourceManager)sfxSources[i]; if (sfx.source != null && !sfx.source.isPlaying) { sfxSources.Remove(sfx); sfx.Destroy(); } } } // If the bgm is not on loop and finishes, update bgmState to match if (bgmState == SoundPlayerState.Playing && !bgmSource.isPlaying) { bgmState = SoundPlayerState.Stopped; } }
// Stop the background music. public void StopBGM() { bgmSource.Stop(); bgmState = SoundPlayerState.Stopped; }