A fix to a Common XNA SoundEffect problem

If you’re developing a game, have you ever tried to stop and start a sound effect repeatedly in quick succession using SoundEffectInstance? If so you probably tried something like:

mySoundEffectInstance.Stop();
mySoundEffectInstance.Play();

And when you did, you noticed something wasn’t right. The sound would either just stop, or just start, not both. You probably searched the web, found some round about ways if fixing it. Well here is what I believe to be the best, no-extra-classes, no-extreme-overhead way of fixing it. Within each class that you want to play one sound effect at a time, but those sound effects may play quite rapidly, use this method:

private void playSFX(SoundEffect sfx)
 {
      mySoundEffectInstance.Stop();
      mySoundEffectInstance = sfx.CreateInstance();
      mySoundEffectInstance.Play();
 }

The one thing you need to make sure of is that the “mySoundEffectInstance” variable is assigned to an instance of a SoundEffect (which could be one of the many in your class) in the constructor, so a nullPointer isn’t thrown. For a look at an example class:

class PlayerMain
    {
        SoundEffect sfx0, sfx1, sfx2, sfx3;
        SoundEffectInstance mySoundEffectInstance;

        public PlayerMain(ContentManager c)
        {   
            sfx0 = c.Load("../../Sounds/SFX/sfx0");
            sfx1 = c.Load("../../Sounds/SFX/sfx1");
            sfx2 = c.Load("../../Sounds/SFX/sfx2");
            sfx3 = c.Load("../../Sounds/SFX/sfx3");

            mySoundEffectInstance = sfx0.CreateInstance();
        }

        public void move(int x)
        {
            switch(x)
            {
                case 0:
                    playSFX(sfx0);
                    break;
                case 1:
                    playSFX(sfx1);
                    break;
                case 2:
                    playSFX(sfx2);
                    break;
                case 3:
                    playSFX(sfx3);
                    break;
            }

        }

        private void playSFX(SoundEffect sfx)
        {
            mySoundEffectInstance.Stop();
            mySoundEffectInstance = sfx.CreateInstance();
            mySoundEffectInstance.Play();
        }

    }

A not very functional class, but one that demonstrates the ability behind playSFX, and how it can fix any problems you may have had.

3 comments

  1. Wonderful data, numerous thanks towards author. It is incomprehensible to me now, but in basic, the usefulness and significance is overwhelming. Thanks yet again and fine luck!

  2. [...] where you left off.Get Your Boyfriend Back Get Your Ex internet marketerfriend Back Get Your Ex Boyfriend Back Get Your Ex Back How To Get Your Ex Back This entry was posted in Uncategorized. Bookmark the permalink. [...]

  3. So ha ha hay , and potent post . I really like this visual and anything related to mistress!

Leave a Reply

Your email address will not be published. Required fields are marked *


2 − = one