hunteroatway17
hunteroatway17

Reputation: 63

C#/XNA Stopping Songs In Different GameStates

I am trying to have music play in my Menu state but cut out after I switch to my Game state.

Upvotes: 1

Views: 1691

Answers (1)

Victor Buendía
Victor Buendía

Reputation: 471

The MediaPlayer class has a Play() and Stop() methods. Here you are the MSDN links:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.play.aspx http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.stop.aspx

If I remember correctly, you can do this:

// Variable declaration
Song menu_song;
Song game_song;
bool isInMenu; //I'll use this instead of the gamestate as example

//In the LoadContent method:
//remember to add the song to your project. In this case is in 
//a folder called "music"
 Content.Load<Song>("music/menuSongName"); 
 Content.Load<Song>("music/gameSongName"); 

//In Update method:

if (isInMenu) 
{
MediaPlayer.Stop(); //Stop the current audio...
MediaPlayer.Play(menu_song); //...and start playing the next.
}
else
{
MediaPlayer.Stop(); //Stop the current audio
MediaPlayer.Play(game_song); //...and start playing the next.
}

I haven't checked this code, if you have some problem please tell me and I'll check it. =) And it's the same with the gamestates, I think you'll know how to adapt this code to your needs.

Upvotes: 1

Related Questions