0
\$\begingroup\$

So in XNA I have this code: Variables:

SoundEffect optionSelected;

In the LoadContent() method:

optionSelected = Content.Load<SoundEffect>("gameSounds\\selectOption");

In the Update() method:

optionSelected.Play();

So when it comes time for the sound to play it never stops looping. I want to hear it only once. How to achieve that? Thanks in advance!

\$\endgroup\$
2
\$\begingroup\$

Well if you are calling Play() every time the update loop runs it will play each time the game updates. However ignoring that to stop it looping you'd do

optionSelected = Content.Load<SoundEffect>("gameSounds\\selectOption");
optionSelected.IsLooped=false;

Hope that helps

EDIT: From the comment thread the solution would be to add a keypress event listener and call Play() when the desired key is pressed.

\$\endgroup\$
  • \$\begingroup\$ Uh... IsLooped is a member of SoundEffectInstance and not SoundEffect. The Play method on the latter is just a convenience method for fire-and-forget sounds which, naturally, don't support looping. \$\endgroup\$ – Andrew Russell Sep 13 '13 at 2:13
  • \$\begingroup\$ Yep this did not work. Any other suggestions? \$\endgroup\$ – PowerUser Sep 13 '13 at 10:35
  • \$\begingroup\$ How are you calling it from your update loop? you might want it after an if statement such as if(isOptionClicked==true) {optionSelected.Play(); isOptionClicked=false;}. Or stick it somewhere else \$\endgroup\$ – user1646196 Sep 13 '13 at 10:44
  • \$\begingroup\$ OK, I used a bool but now when it comes time for the sound to play, well it doesn't play. Reply would be really nice! \$\endgroup\$ – PowerUser Sep 13 '13 at 16:25
  • \$\begingroup\$ You have to set the boolean to true at some point. Are you sure you want the sound in the update loop. If this is for an option being selected then I'm assuming a button is clicked or something of that ilk. If so you could add an event handler to whatever is triggering the sound so when it's clicked it plays instead of keeping it in the update loop \$\endgroup\$ – user1646196 Sep 14 '13 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.