I’m porting a game from Mac OS X to Windows 7, and something about my fade in / out functions broke in the transition. The game is a platformer where different screens are grouped into zones, each with their own music. When the player walks into a different zone, the music from the previous zone fades out, and once the volume has reached zero, the new music fades in. This worked perfectly on Mac OS X, but now the music only fades in and out properly the first time to change zones - after that, the volume jumps from full-blast to zero with no transition. Here’s the code for them, along with the function I call to change music that uses them:
private void fadeOut(double delta)
{
float volume = MediaPlayer.Volume;
volume -= (float)(delta * FADE_SPEED);
if(volume < 0)
{
volume = 0;
isFadingOut = false;
isFadingIn = true;
currentMusic = nextMusic;
MediaPlayer.Stop();
LoveGame.unloadMusic();
LoveGame.loadMusic(currentMusic);
MediaPlayer.Play(LoveGame.backgroundMusic);
}
MediaPlayer.Volume = volume;
}
private void fadeIn(double delta)
{
float volume = MediaPlayer.Volume;
volume += (float)(delta * FADE_SPEED);
if(volume > MAX_VOLUME)
{
volume = MAX_VOLUME;
isFadingIn = false;
}
MediaPlayer.Volume = volume;
}
private void changeMusic(string newMusic)
{
Console.WriteLine("changeMusic(" + newMusic + ")");
if(!nextMusic.Equals(newMusic))
{
nextMusic = newMusic;
isFadingOut = true;
isFadingIn = false;
if(currentMusic == "silence")
{
MediaPlayer.Volume = 0f;
}
}
}
I’ve printed out MediaPlayer.Volume in the console, and it’s incrementing and decrementing as it should be, but the actual volume of the song is jumping from max volume to zero and back. Does anyone have any ideas why it would be doing this? Is it a bug with MediaPlayer in Windows 7, or something?