Search code examples
c#audioxnainstance

XNA/MonoGame soundeffects and music is slowing down my game


What am I doing wrong?

Music and sound effects are slowing down my game so much, there is noticeable lag and stutter. I am using MonoGame.

Music:

Game1.instance.Stop();
Game1.bgEffect.Dispose();
Game1.bgEffect = Content.Load<SoundEffect>("../../../../Content/Sound/track" + Player.Age);
Game1.instance = Game1.bgEffect.CreateInstance();
Game1.instance.Play();

Sound effect:

if (Game1.fireSoundEffect != null)
{
     Game1.fireSoundInstance.Stop();
     Game1.fireSoundEffect.Dispose();
}

Game1.fireSoundEffect = Content.Load<SoundEffect>("../../../../Content/Sound/fire" + Player.Age);
Game1.fireSoundEffect.Play((float)0.3, (float)0.0, (float)0.0);
Game1.fireSoundInstance = Game1.fireSoundEffect.CreateInstance();
Game1.fireSoundInstance.Play();

I have a feeling I'm either not Disposing the object completely, or not stopping the instance from running. How can I fix this?


Solution

  • Looks like you are loading the files from the content pipeline, initializing the objects, and then playing the sounds all at the same time, in your Update loop most likely.

    What you need to do, is load and initialize your content during in your LoadContent function (or any other init function). Then in your Update loop, you would simply call the Play() function when needed.