StepTNT
StepTNT

Reputation: 3967

Audio looping with XNA on Windows Phone 8 while the sound is still playing

I need a way to start/stop a loop while the sound is still playing. I've found that the best way to play multiple sounds together is to use XNA. So I've created a SoundEffectInstance starting from my SoundEffect object who contains the audio clip. Now the problem is that I have a button which should change the state of this clip from "looped" to "non looping" and viceversa.

Here's the problem: it throws an exception sayning that the loop must be set before the first play. So I thought that, while switching from "non looping" to "looped", I could just wait for the sound to stop and then recreate the SoundEngineInstance, setting IsLooped = true and make it start again. This one works, but there's some delay and this makes you lose your timing, so it's quite useless.

What I'm looking for is a way to set or unset the loop while playing the sound and without any kind of delay.

EDIT:

I tried using two SoundEngineInstance, one looped and one not, and simply switch between them using the volume. The problem is that the non looped one just stops after the first play, so if I loop it for two times and then I put it to non loop status, no sound is played because the non looped clip already ended!

Suggestions?

Upvotes: 11

Views: 1308

Answers (2)

congusbongus
congusbongus

Reputation: 14652

From http://msdn.microsoft.com/en-us/library/dd940203(v=xnagamestudio.31).aspx:

  1. Declare a SoundEffect using the method shown in How To: Play a Sound.

  2. Declare a SoundEffectInstance, and set it to the return value of SoundEffect.CreateInstance.

  3. Set SoundEffectInstance.IsLooped to true.

  4. Call SoundEffectInstance.Play to play the looping sound.

You can probably set IsLooped to false when you no longer want it to loop, but only to play to the end.

Upvotes: 0

Ferenc Kun
Ferenc Kun

Reputation: 428

I had the same proplem and I used two instances with the same audio clip to solve it, one with looping and one without it. I switched betwwen the two. It is maybe not the best solution but it worked for me.

Upvotes: 2

Related Questions