In this lecture we're gonna go through the process of playing sound effects and music in our game.
Before doing anything we need to have some audio files to use in our project.
You can use any sound files that you have whether it's MP 3 or that wave but you don't have any available.
I created these two sound files a while back and I'm including them as part of this lecture.
We have flipped out wave and nature that Argh.
This is what that wave sounds like in nature.
The ARG is a music track.
Sounds like this
once you have your files ready we can go into Visual Studio and your project and go to the content folder
and we're going to create a new folder
for sounds okay.
Now when you actually put those files we talked about earlier into this folder so I'm going to copy
these.
Then I'm going to go to the game project file and we're going to find our content folder.
We have our new sounds folder that we created and was going to paste those two files in there.
Next we're going to open up our content that MDG CV or the content pipeline
we want to go over here to add existing folder These are sounds folder
okay and you can see a two full are are two files are in there already.
Now that they're in there we can do A build actually we should save.
And then we can do A build
and as long as you see that it's building sound slash blip that wave and building sounds slash nature
that ARG.
Then you know that it's fine because these two have built now they're ready to use in our game.
So in game one let's see yes we need to load the sounds into our code.
This is really similar to loading any other asset into our code.
Before we do anything though we need to actually include a new package actually going to put it up here
using Microsoft that X and A that framework that audio.
So this package allows us to use sound effects which is what blip that wave is now to love the sounds.
We're going to load blip that wave first and here's something we need to keep in mind we only need to
load the sound once even though we might be using sound effect multiple times say for blip that way.
Let's let's use blip that wave whenever the player shoots a projectile even though they're shooting
lots projectiles.
We only need one reference to the sound file.
We don't need to load it every single time the projectile fires.
We want these sounds to be available throughout all the classes.
So we're going to do is create a static class for this public static class.
We'll call it my sounds
and we'll start off with public static sound effect is the type and we call this projectile sound and
that's our declaration.
This is a nice trick for when you need some data that's available across all your classes or all of
your files.
Because now we just add to say my sounds that protect our sound no matter what file we're in we'll be
able to access it.
Now let's go down and actually load it will load it right here I guess we just say my sounds that projectile
sound equals content that load the tape is sound effect and then we need to give it a path
which is
sounds.
Slash bullet.
And remember you do not put the extension you just say blip not blip that wave.
So now our Sound Effect is loaded into the game.
But we need to actually play it at some point.
And like I mentioned earlier I want this to play whenever the player shoots a projectile.
So let's go in to play that C.S.
and they shoot the projectile right here.
So right afterwards I'm going to do my sounds that projectile sound that play just like that.
This line will play the sound.
And now at this point we're ready to test it
and it works
now you should know that this play function was play method actually has some optional parameters that
we can put in there.
So we'll do this for example one F
zero point five F and zero f remember that the f just clarifies that this is a float.
Now what these parameters actually represent This value represents the volume of the sound effect.
This one represents the pitch for the sound effect.
And this one represents the pan.
So to clarify this is a zero to one value zero being completely quiet and one being full volume same
volume as the sound file itself.
This is the pitch.
So here I signified zero point five f.
That means that the pitch is going to raise by half an octave.
Every one is an octave.
So if I did to f that would mean the pitch would rise by two octaves and you can go down to if I did
if I did negative to f that would mean the pitch drops by two octaves.
I'm actually going to change it back to zero point five and then pan pandas refers to the focus of the
left and right speaker.
And this is a negative one value to one value if it's negative one it's all the way focus on the left
speaker if it's positive one is all the way focus on the right speaker so having it at zero would make
it equal in both so if this changes if I save and start the only thing that changes the pitch
the pitch of each shot is slightly higher now
using the standard sound effect type is probably all you're going to need for your games.
However if you need further control of your sounds.
Look up these sound effect instance class.
It's similar buffer is a bit more control if you need it.
Next we're gonna get our background music playing which is a slightly different process.
We're gonna go back to Game 1 and see us.
And actually since it's a song it operates a little bit differently.
We're going to also include a new package and this one is called Microsoft that X made up framework
dot media MEAA.
Then we can declare our song public static song.
We'll call it BMG music.
And now to load it we'll go down
to here.
We'll do my sounds that B G music equals content that load the type of song and same deal we specify
the path sounds.
Slash nature.
Now you might be wondering why is nature to OG considered to be a song while lip that wave is considered
to be a sound effect.
And this is actually because of the file type that waves are by default considered sound effects and
which are considered songs but this is completely customizable within the content pipeline.
If we click on one of these audio files we can see right down here what is being signified as our processor
so blip is being considered a sound effect.
While nature is considered a song but say we wanted blip to be considered a song as well we could use
this dropdown and change it to a song.
Then we just have to save and build again.
And it would work as a song but we actually went as the sound effects we'll just leave it as is now
that we have music loaded in.
We can actually play it and for demonstration purposes I'm just gonna play it right here what you do
is you do media player that play and then you specify the song you want to play which is my sounds that
BMG music
and with that are songs you play when we start up
and it does
media player also has some other useful methods that we can use such as media player dat stop.
There is also media player dat pause and media player dat resume.
You can use of those methods for different functions.
So if you want to add the level anyone the use to stop you call stop or if maybe you pause the game
then you could choose media player dat pause and it would pass a song and then you can resume right
where the song left off which is pretty neat.
And that about covers everything you need to know about sound effects and songs in modern game.