/// <summary> /// Adds a new audio song. /// </summary> /// <param name="key">The key of the audio song.</param> /// <param name="audioEffect">The audio song.</param> public void AddAudioSong(string key, AudioSong audioSong) { if (audioSongs.ContainsKey(key)) { throw new Exception("They given key is already in use:" + key); } audioSongs.Add(key, audioSong); }
/// <summary> /// Play the audio song. /// </summary> /// <param name="key">The key of the audio song to play.</param> public void PlayAudioSong(string key) { if (!audioSongs.ContainsKey(key)) { throw new KeyNotFoundException("There is no audio song for the key: " + key); } if (activeAudioSong == null) { activeAudioSong = audioSongs[key]; activeAudioSong.VolumeTransitionIn(); } else { nextAudioSong = audioSongs[key]; activeAudioSong.VolumeTransitionOut(); } }
/// <summary> /// Updates all sound effects and songs. /// </summary> /// <param name="gameTime">The game time since the last update.</param> public void Update(GameTime gameTime) { // update all audio effects foreach (var audioEffect in audioEffects.Values) { audioEffect.Update(gameTime); } // update music song if (activeAudioSong != null) { // switch music song, when the transition is over if (nextAudioSong != null) { if (!activeAudioSong.IsVolumeTransitionActive) { if (MediaPlayer.State == MediaState.Playing) { MediaPlayer.Stop(); } activeAudioSong = nextAudioSong; activeAudioSong.Reset(); activeAudioSong.VolumeTransitionIn(); MediaPlayer.Play(activeAudioSong.Song); nextAudioSong = null; } } // adjust music player volume if (activeAudioSong.IsVolumeTransitionActive) { // TODO: check if the frequent volume update causes performance problems MediaPlayer.Volume = audioSettings.MusicVolume * activeAudioSong.CurrentAudioVolumeFactor; } activeAudioSong.Update(gameTime); } }