Switching songs - MediaPlayer lags the game

Posted by Fibericon on Game Development See other posts from Game Development or by Fibericon
Published on 2012-03-21T06:47:24Z Indexed on 2012/03/21 11:39 UTC
Read the original article Hit count: 241

Filed under:
|
|

When the player encounters a boss in the game I'm working on, I want to have the music change. It seems simple enough with the MediaPlayer class to fade out the current song, switch to another, and then fade the new song in. However, at the point where the second song starts, the game freezes for a split second. The songs in question aren't particularly large either - the first song is 1.7mb and the second song is 3.1mb, both mp3 format. This is the code I'm using to do it:

protected void switchSong(GameTime gameTime)
{
   if (!bossSongPlaying)
   {
      MediaPlayer.Volume -= ((float)gameTime.ElapsedGameTime.TotalSeconds/10);
      if (MediaPlayer.Volume < 0.05f)
      {
         MediaPlayer.Play(bossSong);
         MediaPlayer.Volume = 1.0f;
         bossSongPlaying = true;
      }
   }
}

What can I do to eliminate that momentary hang?

© Game Development or respective owner

Related posts about XNA

Related posts about c#