In order to play a sound using the SoundEffect
type, create a variable to hold the loaded sound. Typically this would be an instance variable in the Game
class:
private SoundEffect mySound;
Then, in the LoadContent()
method of the Game
class:
protected override void LoadContent()
{
// load the audio content
mySound = Content.Load("mySound");
}
Finally, whenever the sound needs to be played, just invoke the Play()
method:
bool played = mySound.Play();
If for some reason, such as too many sounds are already playing, the Play()
method may return false
. If the sound started playing successfully, then it will return true
.