Stop the SoundEffect from looping - c#

I'am building a game in C#/XNA, it is an top-view racing game and I want to play a sound effect, when my car bumps into a wall.
The problem is that my sound keeps looping so, when I hit a wall, the song will instantly start playing, but I want it to play, and when it is finished playing, then it can be played again.
Here is the code that takes care of the collision:
if (map[x][y] == 0)
{
car.speed = 0;
crash.Play(); }
Please ask me if I'am not clear about something.
Thanks in advance!

Here is the complete answer to your questions: http://msdn.microsoft.com/en-us/library/dd940203.aspx
SoundEffectInstance instance = soundEffect.CreateInstance();
instance.IsLooped = true;
However, by default, SoundEffect should not be looped...

Call SoundEffect.CreateInstance() to get a SoundEffectInstance. Then set the IsLooped property to false before you call Play:
crashSoundEffectInstance.IsLooped = false;

You may want to look into the SoundEffectInstance class. Here's an example on how to use it properly.
You can check if the sound effect state to see if it's currently playing
Edit: I've added a more complete code, as I felt it was needed. It's roughly the same example as in the link I've provided. (Note: This code is untested)
//In your content load
SoundEffect crash;
crash = Content.Load<SoundEffect>("PathToYourSoundEffect");
SoundEffectInstance sei = crash.CreateInstance();
//In your update code
if(sei.State == SoundState.Stopped || sei.State == SoundState.Paused)
{
sei.Play();
}

Related

How do i flip the value of AudioListener.pause like a bool

I am working on a menu script but I have a little bit of a problem. Whenever I pause my game, I want the music to stop. So I do this:
if (Input.GetKeyDown(KeyCode.Escape))
{
isPaused = !isPaused;
AudioListener.pause =! AudioListener.pause;
}
What this does is it makes the audio works the first time I play the game and stops it forever after that. How Do I Fix This Problem?
Feedback is always appreciated ;)
Since your assigning a "isPaused" variable, have you tried just setting it to equal that?
isPaused = !isPaused;
AudioListener.pause = isPaused;
Im really not sure why your code wouldnt work though. AudioListener.pause is a bool and you should be able to use the "!" function on it.

How can I check if MediaPlayer has finished loading a sound?

I am trying to play multiple sounds at the same time for a simple game using the MediaPlayer class (System.Windows.Media.Mediaplayer).
To play the same sound multiple times at the same time, I created my own class, which loads the same sound many times.
Here is a short example:
sound = new MediaPlayer[count]; //array of MediaPlayers
//...
for (int a = 0; a < count; a++)
{
sound[a] = new MediaPlayer();
sound[a].Open(new System.Uri(sp + pfad)); //load sound
}
Here is my problem:
If I try to play a sound shortly after starting the game either I can't hear the sound or the beginning of the first sound I loaded will start playing (even if I tried to play a completely different sound).
It seems like all these MediaPlayers (I'm loading ~100) need some time to load the sounds.
Therefore I'm looking for a way to check if the loading is done.
Loading or playback? When the media has finished playback it rises the MediaEnded event, when buffering has finished it rises BufferingEnded event. MediaPlayer events.

Song doesn't play all the time in Monogame

Heey,
We created a game with Monogame but we got the following problem.
We got a themesong that plays when you have loaded the game now is the problem that the themesong sometimes plays but sometimes just doesn't. We convert it by the XNA pipeline to a wma and load it into our game with the .xnb together but just sometimes the music doesn't wanna start.
We just use the standard code for starting a song and all of this code does fire.
internal void PlayBackgroundMusic()
{
if (MediaPlayer.GameHasControl)
{
MediaPlayer.Volume = mainVolume;
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
}
}
We also use SoundEffects but these work 100% of the time it's only the song that won't play everytime you start. Windows RT runs it fine by the way.
Make sure that the debugger gets into the if statement through debugging (or remove the statement temporarily). Another possibility might be that the function is running before the game is fully initialized. You could try delaying the function until the game has been fully loaded.
PS: I can't comment on questions yet so here's an answer.
Edit:
Alright, after some messing around with the Song class and looking in the implementation in MonoGame I came to the conclusion that the SoundEffect class is easier to use and better implemented.
backgroundSound = Content.Load<SoundEffect>("alarm");
protected override void BeginRun()
{
// I created an instance here but you should keep track of this variable
// in order to stop it when you want.
var backSong = backgroundSound.CreateInstance();
backSong.IsLooped = true;
backSong.Play();
base.BeginRun();
}
I used this post: using BeginRun override to play the SoundEffect on startup.
If you want to play a song, use the Song class. But make sure you are using ".mp3" instead of ".wma" before converting them into ".xnb".
backgroundMusic = Content.Load<Song>("MainTheme");
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;
See MSDN.

mediaelement keep playing after lock

I have a medialement with a url-source which streams a radio station. Everything works fine and music plays as expected! When I press the shutdown button and the phone locks, the streaming stops. How can I fix that? Even if I press the "flag" button, I see my main screen but the music stops :/
thanks in advance
You must use the BackgroundAudioPlayer to accomplish this.
See this msdn article for more info and my post explaining some gotchas of the BackgroundAudioPlayer
Taking the sample from the msdn link. I changed the PlayTrack method to:
private void PlayTrack(BackgroundAudioPlayer player)
{
var track = new AudioTrack(
new Uri("http://m1.onweb.gr/1055rock"),
"Online",
"Music",
string.Empty,
null,
string.Empty,
EnabledPlayerControls.Pause);
if (player != null)
{
player.Track = track;
}
}
And I get the errors noted below. How are you trying to start the player?

Playing a second SoundEffect after the first one has finished

In my app I'm using SoundEffect to play some sounds. I want to know if there's a way of knowing when a SoundEffect finished its run so a second one will start right after.
This scenario is not supported out of the box by XNA as far as i know.
The SoundEffect class exposes a Duration property that you might use to achieve what you're after.
Build some "SoundManager" class (basically a simple scheduler), that will do all the fancy coordination of sounds playback.
This class will hit off a SoundEffect playback, scheduling the next one to occur exactly after Duration had elapsed.
the old question, but i had to do the same thing today, i tried the method based on duration but finally stopped with own solution which seems the simplest for me:
1) create static helper class let's say 'SoundsManager' as in lysergic-acid's answer.
2) create a Queue of SoundsEffectInstanses and play first of them ("_sheduledSounds" in code below).
3) Update class with each game loop and check if soundinstance is stopped:
public static void Update()
{
if (_sheduledSounds != null && _sheduledSounds.Count > 1)
{
if (_sheduledSounds.Peek().State == SoundState.Stopped)
{
_sheduledSounds.Dequeue();
_sheduledSounds.Peek().Play();
}
}
}
Set this
BufferDescription.GlobalFocus flag = True;
This will tell a SoundEffect finished its run.

Categories

Resources