IEnumerator MonitorSequence(string audioTag) { int origClipID = musicPlayer.clip.GetInstanceID(); float waitSecs = musicPlayer.clip.length; if (fadeOutAudio) { waitSecs -= 5.0F; // account for fadeOut time } if (waitSecs <= 0) { waitSecs = 1.0F; } yield return(new WaitForSeconds(waitSecs)); if (musicPlayer.clip.GetInstanceID() == origClipID && playSequence && playState == AUDIO_PLAY_STATE.Playing) { // if the audio tag has changed, reset the sequence index // (this shoud not happen, as the coroutine is stopped // if/when the tag changes, but just in case): if (audioTag != lastTag) { clipSequenceIndex = 0; } numSequencePlays++; int numClips = NumClipsForTag(audioTag, audioClips); if ((playLoop) || (playRandom && numClips > numSequencePlays) || (!playRandom && (numClips - 1) > clipSequenceIndex)) { if (fadeOutAudio) { fadeState = AUDIO_FADE_STATE.FadingOut; playState = AUDIO_PLAY_STATE.Stopping; StartCoroutine(FadeOut()); do { yield return(new WaitForSeconds(1.0f)); }while (playState != AUDIO_PLAY_STATE.Stopped); } if (sequenceClipDelaySeconds > 0) { // configured to add some silence between clips in a sequence yield return(new WaitForSeconds(sequenceClipDelaySeconds)); } Play(lastTag); // this will restart the sequence coroutine if it is still enabled } else { // reached the end of a sequence that should not be looped, play the potential outro clipSequenceIndex = -1; Stop(); } } }
IEnumerator FadeOut() { float originalVolume = targetVolume; //masterVolume; do { musicPlayer.volume = musicPlayer.volume - 0.02F; yield return(new WaitForSeconds(0.1F)); // fades out faster than it fades in, which is typically desirable }while (musicPlayer.volume > 0.0F && (fadeState == AUDIO_FADE_STATE.FadingOut || fadeState == AUDIO_FADE_STATE.Crossfading) && (playState == AUDIO_PLAY_STATE.Stopping || playState == AUDIO_PLAY_STATE.PlayingOutro)); if (fadeState == AUDIO_FADE_STATE.FadingOut) { fadeState = AUDIO_FADE_STATE.NoFade; musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; musicPlayer.volume = originalVolume; } }
IEnumerator PlayOutro(string audioTag) { ManagedAudioClip mc = GetOutroForTag(audioTag); playState = AUDIO_PLAY_STATE.PlayingOutro; musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); yield return(new WaitForSeconds(musicPlayer.clip.length)); if (playState == AUDIO_PLAY_STATE.PlayingOutro) { musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; } musicPlayer.loop = originalLoop; }
IEnumerator PlayIntro(string audioTag) { ManagedAudioClip mc = GetIntroForTag(audioTag); musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.PlayingIntro; yield return(new WaitForSeconds(musicPlayer.clip.length)); if (playState == AUDIO_PLAY_STATE.PlayingIntro) { // play original target file ManagedAudioClip mclip = GetClipForTag(audioTag, -1); musicPlayer.clip = mclip.audioClip; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; } musicPlayer.loop = originalLoop; }
public void Stop() { if (!musicPlayer.isPlaying || playState == AUDIO_PLAY_STATE.PlayingOutro) { return; } StopCoroutine("MonitorSequence"); numSequencePlays = 0; if (fadeOutAudio) { if (fadeState == AUDIO_FADE_STATE.FadingOut && playState == AUDIO_PLAY_STATE.Stopping) { // coroutine already in progress, let it exit and reset volume playState = AUDIO_PLAY_STATE.Stopped; } else { fadeState = AUDIO_FADE_STATE.FadingOut; playState = AUDIO_PLAY_STATE.Stopping; StartCoroutine(FadeOut()); } } else { fadeState = AUDIO_FADE_STATE.NoFade; if (playOutros && NumClipsForTag(lastTag, outroClips) > 0) { StartCoroutine(PlayOutro(lastTag)); } else { musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; } } }
IEnumerator PlayOutro(string audioTag) { ManagedAudioClip mc = GetOutroForTag(audioTag); playState = AUDIO_PLAY_STATE.PlayingOutro; musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); yield return new WaitForSeconds(musicPlayer.clip.length); if (playState == AUDIO_PLAY_STATE.PlayingOutro) { musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; } musicPlayer.loop = originalLoop; }
IEnumerator PlayIntro(string audioTag) { ManagedAudioClip mc = GetIntroForTag(audioTag); musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.PlayingIntro; yield return new WaitForSeconds(musicPlayer.clip.length); if (playState == AUDIO_PLAY_STATE.PlayingIntro) { // play original target file ManagedAudioClip mclip = GetClipForTag(audioTag, -1); musicPlayer.clip = mclip.audioClip; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; } musicPlayer.loop = originalLoop; }
IEnumerator MonitorSequence(string audioTag) { int origClipID = musicPlayer.clip.GetInstanceID(); float waitSecs = musicPlayer.clip.length; if (fadeOutAudio) waitSecs -= 5.0F; // account for fadeOut time if (waitSecs <= 0) waitSecs = 1.0F; yield return new WaitForSeconds(waitSecs); if (musicPlayer.clip.GetInstanceID() == origClipID && playSequence && playState == AUDIO_PLAY_STATE.Playing) { // if the audio tag has changed, reset the sequence index // (this shoud not happen, as the coroutine is stopped // if/when the tag changes, but just in case): if (audioTag != lastTag) { clipSequenceIndex = 0; } numSequencePlays++; int numClips = NumClipsForTag(audioTag, audioClips); if ((playLoop) || (playRandom && numClips > numSequencePlays) || (!playRandom && (numClips-1) > clipSequenceIndex)) { if (fadeOutAudio) { fadeState = AUDIO_FADE_STATE.FadingOut; playState = AUDIO_PLAY_STATE.Stopping; StartCoroutine(FadeOut()); do { yield return new WaitForSeconds(1.0f); } while (playState != AUDIO_PLAY_STATE.Stopped); } if (sequenceClipDelaySeconds > 0) { // configured to add some silence between clips in a sequence yield return new WaitForSeconds(sequenceClipDelaySeconds); } Play(lastTag); // this will restart the sequence coroutine if it is still enabled } else { // reached the end of a sequence that should not be looped, play the potential outro clipSequenceIndex = -1; Stop(); } } }
IEnumerator FadeOut() { float originalVolume = targetVolume; //masterVolume; do { musicPlayer.volume = musicPlayer.volume - 0.02F; yield return new WaitForSeconds(0.1F); // fades out faster than it fades in, which is typically desirable } while (musicPlayer.volume > 0.0F && (fadeState == AUDIO_FADE_STATE.FadingOut || fadeState == AUDIO_FADE_STATE.Crossfading) && (playState == AUDIO_PLAY_STATE.Stopping || playState == AUDIO_PLAY_STATE.PlayingOutro)); if (fadeState == AUDIO_FADE_STATE.FadingOut) { fadeState = AUDIO_FADE_STATE.NoFade; musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; musicPlayer.volume = originalVolume; } }
public void Stop() { if (!musicPlayer.isPlaying || playState == AUDIO_PLAY_STATE.PlayingOutro) return; StopCoroutine("MonitorSequence"); numSequencePlays = 0; if (fadeOutAudio) { if (fadeState == AUDIO_FADE_STATE.FadingOut && playState == AUDIO_PLAY_STATE.Stopping) { // coroutine already in progress, let it exit and reset volume playState = AUDIO_PLAY_STATE.Stopped; } else { fadeState = AUDIO_FADE_STATE.FadingOut; playState = AUDIO_PLAY_STATE.Stopping; StartCoroutine(FadeOut()); } } else { fadeState = AUDIO_FADE_STATE.NoFade; if (playOutros && NumClipsForTag(lastTag, outroClips) > 0) { StartCoroutine(PlayOutro(lastTag)); } else { musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; } } }
public AudioClip Play(string audioTag, int clipIndex) { StopCoroutine("MonitorSequence"); musicPlayer.Stop(); if (fadeState == AUDIO_FADE_STATE.FadingIn || fadeState == AUDIO_FADE_STATE.FadingOut) { fadeState = AUDIO_FADE_STATE.NoFade; masterVolume = targetVolume; } musicPlayer.volume = masterVolume; // if looping in sequence, it is at sequence level, not clip level musicPlayer.loop = (playSequence) ? false : playLoop; if (NumClipsForTag(audioTag, audioClips) == 0) { playState = AUDIO_PLAY_STATE.Stopped; return null; } if (!playSequence && !playRandom && !playLoop && clipIndex == -1) { // play first clip once and then let it stop clipIndex = 0; } if (!playSequence || audioTag != lastTag) { // reset sequenceIndex clipSequenceIndex = -1; } ManagedAudioClip mclip = GetClipForTag(audioTag, clipIndex); musicPlayer.clip = mclip.audioClip; if (fadeInAudio) { // NOTE: Crossfading works only when playing uncompressed intros & outros, so it's unsupported for now fadeState = AUDIO_FADE_STATE.FadingIn; StartCoroutine(FadeIn()); } else { targetVolume = masterVolume; } musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; // if there are any random layers to play with this tag, start the coroutine to handle it if (NumClipsForTag(audioTag, layeredClips) > 1) { StartCoroutine(PlayLayers(audioTag)); } if (playSequence) { StartCoroutine(MonitorSequence(audioTag)); } lastTag = audioTag; return mclip.audioClip; }
public void Pause() { musicPlayer.Pause(); playState = AUDIO_PLAY_STATE.Paused; }
public AudioClip Play(string audioTag, int clipIndex) { StopCoroutine("MonitorSequence"); musicPlayer.Stop(); if (fadeState == AUDIO_FADE_STATE.FadingIn || fadeState == AUDIO_FADE_STATE.FadingOut) { fadeState = AUDIO_FADE_STATE.NoFade; masterVolume = targetVolume; } musicPlayer.volume = masterVolume; // if looping in sequence, it is at sequence level, not clip level musicPlayer.loop = (playSequence) ? false : playLoop; if (NumClipsForTag(audioTag, audioClips) == 0) { playState = AUDIO_PLAY_STATE.Stopped; return(null); } if (!playSequence && !playRandom && !playLoop && clipIndex == -1) { // play first clip once and then let it stop clipIndex = 0; } if (!playSequence || audioTag != lastTag) { // reset sequenceIndex clipSequenceIndex = -1; } ManagedAudioClip mclip = GetClipForTag(audioTag, clipIndex); musicPlayer.clip = mclip.audioClip; if (fadeInAudio) { // NOTE: Crossfading works only when playing uncompressed intros & outros, so it's unsupported for now fadeState = AUDIO_FADE_STATE.FadingIn; StartCoroutine(FadeIn()); } else { targetVolume = masterVolume; } musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; // if there are any random layers to play with this tag, start the coroutine to handle it if (NumClipsForTag(audioTag, layeredClips) > 1) { StartCoroutine(PlayLayers(audioTag)); } if (playSequence) { StartCoroutine(MonitorSequence(audioTag)); } lastTag = audioTag; return(mclip.audioClip); }