# Solved: XNA Game: playing and stopping audio



## DYS (Dec 29, 2008)

Hi all.

I'm currently having to make an XNA game for a University project.

_One_ problem I am having is playing and stopping audio.

Right now I can successfully play a .WAV file, using this code:

```
ContentManager contentManager = new ContentManager(this.Services, @"Content\Audio");  //<--- The destination of the sound file
                soundEffect = contentManager.Load<SoundEffect>(openingAudio);
                soundEffect.Play();
```
However, it will just play the same audio throughout the whole game. I want to be able to just play this piece of audio during the opening screen and other menus, but play something else during the actual gameplay. Therefore, I want to be able to tell the audio to stop when it reaches another Gamestate (I have implemented gamestates), and then play something else in that other Gamestate.

If you require any more code, then just holla.

Thanks in advance


----------



## DaBeers (Oct 31, 2008)

There are several approaches:

(1st)
Look into creating an Effect Instance. <soundEffect> is best used for a '1 shot' sound effect. By creating an effect instance, you should gain access to new properties that would solve your problem. (Like being able to set looping to false.)

```
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] playSound([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SoundEffect[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] s, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]float[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] screenPosition,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]float[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] volume)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]screenPosition = screenPosition - (screenWMax / 2);[/SIZE]
[SIZE=2]screenPosition = screenPosition / (screenWMax / 2);[/SIZE]
[SIZE=2]Bonk = s.CreateInstance();[/SIZE]
[SIZE=2]Bonk.Pan = screenPosition;[/SIZE]
[SIZE=2]Bonk.Volume = volume;[/SIZE]
[SIZE=2]Bonk.Play();[/SIZE]
[SIZE=2]}[/SIZE]
```
(2nd) 
Use the <Song> data type. This works better for playing extended tracks.

```
[SIZE=2]devilsSlide = Content.Load<[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Song[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"sounds/song"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MediaPlayer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].IsRepeating = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MediaPlayer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Volume = (.3f);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MediaPlayer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Play(devilsSlide);[/SIZE]
```
This works pretty well if you want music to respond to game state, or want to randomize things.

(3rd) 
Grab a tutorial for XAct3. This allows you to add or alter parameters for your sounds. It takes further implementation than you have so far, so it's not the easy answer, but the control you gain will be appreciated later. In the past I have avoided using this and was never held back, but once you get to know it, it is a pretty good tool offering options that would be a nightmare to code for.

Good luck


----------



## DYS (Dec 29, 2008)

DaBeers said:


> There are several approaches:
> 
> (1st)
> Look into creating an Effect Instance. <soundEffect> is best used for a '1 shot' sound effect. By creating an effect instance, you should gain access to new properties that would solve your problem. (Like being able to set looping to false.)
> ...


Cheers. I'll give the second option a go.

Can I put the MediaPlayer.Play(); command in any GameState section in my Update method? Also, is there a Stop function?

I've been trying to use XAct, but I've tried three times now and when I try to compile the code it just tells me it's the wrong .xap version, and doesn't offer anything else as an alternative.


----------



## DaBeers (Oct 31, 2008)

The media player should be accessible from any class as long as you set it up properly (public). This should cover your game states no matter how you are implementing them. Also, the player does have a stop function as well as pause and many others. 

We have had some problems with XACT as well but we have not been able to duplicate it over multiple systems. Some have versioning messages, others have problems finding files. I know that the .wavs must be added to Content\Audio before adding them to the XACT project. My last project worked, but once all was implemented in code, I had to comment out the Content.Load lines, build the project, then uncomment the lines. I did this by accident after getting errors, then things worked fine. It is kind of a pain, isn't it? I was told that if all is set up correctly, the .wavs should show up in your debug file after building, however, mine worked without having to check that out. I've only ever used it in one project...like I said, much can be done without the hassle, and since I have a feeling your kind of new to messing with sounds, you will probably be happy with MediaPlayer and SoundEffectInstance for a while.

Anyway, I hope this helps get you on track,

GL


----------



## DYS (Dec 29, 2008)

DaBeers said:


> The media player should be accessible from any class as long as you set it up properly (public). This should cover your game states no matter how you are implementing them. Also, the player does have a stop function as well as pause and many others.
> 
> We have had some problems with XACT as well but we have not been able to duplicate it over multiple systems. Some have versioning messages, others have problems finding files. I know that the .wavs must be added to Content\Audio before adding them to the XACT project. My last project worked, but once all was implemented in code, I had to comment out the Content.Load lines, build the project, then uncomment the lines. I did this by accident after getting errors, then things worked fine. It is kind of a pain, isn't it? I was told that if all is set up correctly, the .wavs should show up in your debug file after building, however, mine worked without having to check that out. I've only ever used it in one project...like I said, much can be done without the hassle, and since I have a feeling your kind of new to messing with sounds, you will probably be happy with MediaPlayer and SoundEffectInstance for a while.
> 
> ...


Hmm, yeah that sounds good. But if there is no stop/pause function, if I have one sound file playing in the main menu, then have another sound file playing during the gameplay, the first sound file from the main menu will still be playing, won't it?

Yeah it is a pain. What a fail on behalf of Microsoft. Nice of them to release something that has bugs, ha ha.


----------



## DYS (Dec 29, 2008)

Also, got another question.

With the MediaPlayer lark, where do I put all the code? Lol.

Is "devilSlide" just an identifier? Then in the brackets I put the location of the .WAV file and then the file name?

And then I can put the play function wherever I want?

Cheers.


----------



## DaBeers (Oct 31, 2008)

DYS said:


> Hmm, yeah that sounds good. But if there is no stop/pause function, if I have one sound file playing in the main menu, then have another sound file playing during the gameplay, the first sound file from the main menu will still be playing, won't it?
> 
> Yeah it is a pain. What a fail on behalf of Microsoft. Nice of them to release something that has bugs, ha ha.


Actually, I think you may have misunderstood. Everything *but* the basic <SoundEffect> class handles pause and stop. MediaPlayer or XACT *would* be able to do this for you.

'devilsSlide' is actually the name of the song. The only decloration I didn't show you was declared as a global (class level) variable, Song devilsSlide;. Once that datatype is declared, it is accessed through the code I gave in my example using MediaPlayer as the control. You would write something like this when you change game state, assuming devilsSlide was already playing:

MediaPlayer.Stop(); //Not 100% sure this is required to change tracks. You may be able to just play the second track.
MediaPlayer.Play(track2);// assuming that track2 is initialized the same way devilsSlide was.

Pretty slick, huh?.. and what 6 lines of code? There are a whole bunch of other functions of Media Player as well, but I have not dug much further than this.


----------



## DYS (Dec 29, 2008)

Ah fair enough. Cheers.

But where do I declare all this stuff? Lol, sorry, I must sound like a real n00b, which I am really, and I just want to get this right.

Cheers.


----------



## DaBeers (Oct 31, 2008)

Well, at this point I'm going to hang you. I know your a student and some things must be figured out on your own. However, if you run into problems, feel free to share your code back (PM if it's a secret). I will let you know where you are going wrong. 

I'll give you a hint: I answered that question already.

The goal here is to help, not do the work for you. I charge $150 an hour as a consultant. If that's what your looking for, let me know. 

(BTW, in case this is the last time we talk, love the avatar, gigity)(working sounds into XNA is less restrictive than hosting an avatar)


----------



## DYS (Dec 29, 2008)

Ha, alright then, fair play.

Thanks for the help. I'll be in touch if I have any snags.


----------



## DaBeers (Oct 31, 2008)

Thanks for understanding.


----------



## DYS (Dec 29, 2008)

All good man.

Anyway, I've encountered a problem now.

I've declared the audio tracks at the beginning of the class as *Song* objects.

I've loaded the two audio tracks I wish to use at the moment in the LoadContent() method.

I can get one track to play, but when I go to another game state and wish to play another track, it just freezes on the screen but with no audio, and it doesn't respond to any key commands when it should.

If you want code, let me know.

Cheers.


----------



## DYS (Dec 29, 2008)

Ah success! My tutor helped me out with the freezing problem, so it's golden now. Hopefully I can sort it myself now when I try and put music in other places.

Thanks very much for all the help DaBeers.

If I have another problem, could I PM you rather than making another thread? Or just make another thread?

Cheers.


----------

