[SOLVED] I want to repeat a SoundEffect a specified # of times

Hi all:

I have been knocking the rust off my XNA/Monogame skills recently, and have been working on a simple game where I want a bell to sound based on how the player does. For instance, if they strike 3 targets, I want the bell to ring 3 times, or 5 times if they strike 5 targets etc. I hacked together a bit of code that works:

public static void RepeatNamedSound(string name, int times)
    {
        int counter = 0;
        if (namedEffects.Count > 0)
        {
            if(namedEffects[name] != null)
            {
                while(counter<times)
                {
                    if(namedEffects[name].State != SoundState.Playing)
                    {
                        namedEffects[name].Play();
                        counter++;
                    }
                }
            }
        }
    }

But this of course has the effect of “locking” my game until the sound finishes playing the specified # of times. So when I call this function, I just ended up creating a thread to do it like so:

Thread thread = new Thread(() => AudioManager.RepeatNamedSound("ShortBell", counter));
thread.Start();

This seems a bit over-complicated to me for such a simple request, but the only other way I could think of to do it was to make an Update() function on my audio manager to run every frame and check a queue of requested sounds for what has played/remains etc. I had wanted to make my audio manager static to function in a kind of fire-and-forget way, so I ended up taking the other approach.

Has anyone else done this? Is there something simple I’m missing here, such as a way to specify the # of times IsLooped = true will loop a sound? I know in XACT this was possible back in the day.

Thanks in advance for reading and your help!

I honestly think this would be a better solution than creating a thread for the sound effects. You could just have the audio manager have one call to audio.Update() in the update loop, and then have a method called PlaySound() which takes the sound you want to play and the number of times you want to play it, and just let audio.Update() handle the implementation. That would still be pretty fire-and-forget.

If you don’t want to do that, then I can’t think of any other way than the thread, though I’m not familiar with XACT and I know that some XACT work is being done on the framework.

I agree with @AJP. I would definitely go with an update call and have some audio manager handle this rather than a seperate thread that’s just busy waiting most of the time. (I don’t know about XACT support for this either)

Alright then, as long as I’m not missing some basic function to do what I want, I had thought that maybe an Update() would be a better solution. Thanks for the replies. :grin:

Have a look at the event queue pattern in this book. The following chapter is also a great read about using services such as audio manager etc. In fact the entire book is a great read.