private IEnumerator <WaitForSeconds> PlayMusic(string file, float volume = 1f)
        {
            if (file != currentSongFullName)
            {
                currentSongFullName = file;
                if (outputDevice.PlaybackState == PlaybackState.Playing)
                {
                    fader.BeginFadeOut(1500);
                    yield return(new WaitForSeconds(1.5f));

                    outputDevice.Stop();
                    currentSong = null;
                }
                currentSong        = new AudioFileReader(file);
                currentSong.Volume = volume * globalMusicVolume;
                var looper = new LoopStream(currentSong, shouldLoop);
                fader = new FadeInOutSampleProvider(new WaveToSampleProvider(looper));
                outputDevice.Init(fader);
                #if DEBUG
                Debug.Log("====== Now Playing: " + file);
                #endif
                outputDevice.Play();
                songPaused = false;
            }
            else
            {
                #if DEBUG
                Debug.Log("PlayMusic: Already playing: " + file);
                #endif
            }
        }
Example #2
0
        private IEnumerator <WaitForSeconds> PlayMusic(string file, float volume = 1f)
        {
            if (outputDevice.PlaybackState == PlaybackState.Playing)
            {
                fader.BeginFadeOut(1500);
                yield return(new WaitForSeconds(1.5f));

                outputDevice.Stop();
                currentSong = null;
            }
            if (file == null)
            {
                //no new music was selected, so return to OST pool
                //If already playing OST music from previous scene,
                //then no need to unmute normal music again
                if (currentSongFullName != null)
                {
                    currentSongFullName = null;
                    unmuteNormalMusic();
                }
                #if DEBUG
                Debug.Log("PlayMusic: Playing music from OST");
                #endif
            }
            else
            {
                if (file != currentSongFullName)
                {
                    //new music provided, play that music.
                    //if we were already playing new music,
                    //then don't mute normal volume again
                    if (currentSongFullName == null)
                    {
                        muteNormalMusic();
                    }
                    currentSongFullName = file;
                    currentSong         = new AudioFileReader(file);
                    currentSong.Volume  = volume * globalMusicVolume;
                    var looper = new LoopStream(currentSong, shouldLoop);
                    fader = new FadeInOutSampleProvider(new WaveToSampleProvider(looper));
                    outputDevice.Init(fader);
                    #if DEBUG
                    Debug.Log("====== Now Playing: " + file);
                    #endif
                    outputDevice.Play();
                    songPaused = false;
                }
                else
                {
                    #if DEBUG
                    Debug.Log("PlayMusic: Already playing: " + file);
                    #endif
                }
            }
        }