This course will be retired on January 24, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Every movie needs a score and every game needs a sound track. Let's add some background music to the title scene and game play scene to make our game a bit more interesting to listen to.
-
0:00
So, background music is not as simple as playing a sound effect, because
-
0:05
a sound effect is simply an ASCI action and you run that action.
-
0:09
Now with background music we have to tap into the AV foundation framework.
-
0:14
Um,don't worry, I'm gonna walk you through the whole process.
-
0:17
So, back in our Space Cap Assets folder, in the
-
0:20
Music folder you'll see that we have three separate MP3 files.
-
0:25
So, this is going to be for our Start screen.
-
0:27
So, if you press the Space bar, you can hear that, it's a pretty funky beat.
-
0:33
[MUSIC]
-
0:34
And that's the Game placing with that deep thump, which, I kinda dig.
-
0:39
[MUSIC]
-
0:41
And then you have a game over MP3 file.
-
0:43
So, what we wanna do here is we wanna select all these
-
0:46
three MP3 files and we'll drag them over to our Sounds folder.
-
0:51
Once again making sure that you have copy
-
0:53
items selected, and you have add to target selected.
-
0:57
So hit Finish.
-
0:57
All right, next thing what we'll do is we'll add our foundation framework.
-
1:03
So click on Space Cat as the project and where it says Linked Frameworks
-
1:10
and Libraries, click on the plus sign and look for AVFoundation.
-
1:17
There, that's the first one.
-
1:18
So you double click on that and you'll see that it's been added to your project.
-
1:23
So, head on over to your THTitleScene.
-
1:28
The first thing we'll do is we'll import.
-
1:30
So #import AvFoundation, Foundation.
-
1:36
Next, what we'll do is we'll have to set up our Player.
-
1:40
So, before we can set up our Player, let's create a
-
1:43
property, so that we can reference that Player from different methods.
-
1:49
So, we'll say property nonatomic AVAudioPlayer.
-
1:53
They have different players.
-
1:55
There's a video player, there's a player.
-
1:57
So, make sure that you select the AVAudioPlayer.
-
2:00
I will call this backgroundMusic.
-
2:04
And then in our initWithSize method we'll set up our background music.
-
2:08
So we'll say music is equal to AVAudioPlayer alloc.
-
2:15
And then say, initWithContentsOfURL.
-
2:18
Now, we'll need a URL that points to the start screen MP3 file.
-
2:24
Cuz that's the MP3 file that we're gonna play for our title scene.
-
2:28
Now remember, background basically just loops continuously in the background.
-
2:33
So we need not worry about the file format.
-
2:36
Here you can easily use the MP3 file format because once it
-
2:39
loads it, it's just gonna loop it over and over and over again.
-
2:43
It's not going to be playing multiple of these sounds at the
-
2:46
same time or playing a hundred of these sounds at the same time.
-
2:50
So we can work with the MP3 file format but we
-
2:54
need to give to AVI audio player the location of this file.
-
2:59
So let's create a NSURL.
-
3:02
We'll see URL is equal to NSBundle, mainBundle.
-
3:10
Because this is going to be bundled with our application,
-
3:13
so we need to look for it in our application bundle.
-
3:15
We'll say URLForResource, and then give it
-
3:23
the name StartScreen with extension MP3.
-
3:31
And this is going to return us a URL.
-
3:34
And now you can easily use this
-
3:36
URL in our initWithContentsOfFiles, so we'll say initWithContentsOfURL:url.
-
3:43
Error, we'll just pass it nil.
-
3:47
Next we'll do self.backgroundMusic.numberOfLoops.
-
3:54
So number of times that you want this music to loop.
-
3:57
In our case, it's indefinitely until the player moves on to the next screen.
-
4:02
So we'll just say minus one.
-
4:04
Which is repeat infinitely.
-
4:06
And the last thing we need to do is we need to call a method prepareToPlay.
-
4:12
So essentially this is going to load up the file from that URL
-
4:16
and it's just going to have it ready so we can play it.
-
4:19
Now when do we play this?
-
4:21
Well we can't play it in the init method.
-
4:23
We want to play it as soon as that scene is drawn on the screen, and for that we're
-
4:29
going to use a method, or we're going to override that method, didMoveToView.
-
4:38
So essentially didMoveToView is kind of like viewDidLoad for your view controller.
-
4:44
So this would be the ideal place for us to play that background music.
-
4:48
So we'll do self.backgroundMusic play.
-
4:53
Now of course if you've played background music somewhere you do need to stop it.
-
4:58
So the minute the player touches our screen
-
5:01
we're going to stop our background music, because we're
-
5:04
going to play another background track the minute
-
5:07
the user goes over to the game play scene.
-
5:10
So let's run our game.
-
5:11
[MUSIC]
-
5:13
And there, we get this nice little track playing the minute the scene loads.
-
5:17
[SOUND] Isn't that a funky little jam?
-
5:20
All right.
-
5:22
Now onto our game play scene.
-
5:24
[BLANK_AUDIO]
-
5:26
So, similarly we're going to first import our AVFoundation.
-
5:32
[BLANK_AUDIO]
-
5:41
And then next we're gonna set up a property for our background music.
-
5:45
I'm thinking we'll just call this backgroundMusic.
-
5:56
And then in our setupSounds.
-
5:58
Cuz we have this nice little method called setupSounds.
-
6:01
This is where we're going to set up our sound.
-
6:04
So, so, I am just going to head on over to the title scene and I am just gonna
-
6:09
copy all that code, because you know, I mean, the
-
6:12
only thing that changes is the name of the file.
-
6:16
So, I am just going to change that to game play.
-
6:21
And once again, we have our prepareToPlay, so all we need to
-
6:26
do is, we need to make sure that we have our didMoveToView.
-
6:34
So we'll do didMoveToView, and we'll
-
6:38
do self.backgroundMusic and call the play method.
-
6:42
So the minute the player moves from the title scene
-
6:46
to the gameplay scene he'll hear a different background music.
-
6:50
[MUSIC]
-
7:01
So quite frankly I love the game play scene because it has
-
7:04
that deep thump to it, but that might just be a personal preference.
-
7:09
So there you have it.
-
7:10
We successfully added background music and sound effects to our game.
You need to sign up for Treehouse in order to download course files.
Sign up