1
00:00:00,672 --> 00:00:08,662
[BLANK_AUDIO]

2
00:00:08,662 --> 00:00:13,290
This is our final lecture in the XNA audio
module.

3
00:00:13,290 --> 00:00:18,309
So, as a final reminder of the module 12
learning objectives, here you go.

4
00:00:20,200 --> 00:00:22,180
Last time, we learned how we could

5
00:00:22,180 --> 00:00:26,790
integrate exact audio assets into our XNA
games.

6
00:00:26,790 --> 00:00:29,630
The problem is that as I record these

7
00:00:29,630 --> 00:00:34,110
lectures, MonoGame doesn't support using
those exact audio assets

8
00:00:34,110 --> 00:00:35,980
in MonoGame games.

9
00:00:35,980 --> 00:00:40,620
So we need a different way to add music
and sound effects to our MonoGame

10
00:00:40,620 --> 00:00:45,230
games, and that's what I'm going to cover
today, is how we can actually do that.

11
00:00:45,230 --> 00:00:48,800
So we're essentially going to be doing the
same game that we did last

12
00:00:48,800 --> 00:00:53,590
time using exact audio, though this time
we'll do it without using exact audio.

13
00:00:53,590 --> 00:00:56,840
I've made significant changes for the
audio part of

14
00:00:56,840 --> 00:00:59,270
the game, and then commented them all out
so

15
00:00:59,270 --> 00:01:03,460
that I can reveal them to you as we go
through this lecture.

16
00:01:03,460 --> 00:01:05,740
So, let's go right to the code.

17
00:01:05,740 --> 00:01:07,050
We'll start at the game one class.

18
00:01:08,190 --> 00:01:09,920
And I'll show you that I have some

19
00:01:09,920 --> 00:01:13,730
additional or different fields declared at
the top.

20
00:01:14,940 --> 00:01:19,510
And let's start, we're going to do it in a
slight different order, because music

21
00:01:19,510 --> 00:01:25,450
is actually more complicated this way than
it was when we were doing the exact stuff.

22
00:01:25,450 --> 00:01:31,010
So we will do music last.
We'll do our button click first.

23
00:01:31,010 --> 00:01:37,280
So the big idea behind doing sound effects
without using exact

24
00:01:37,280 --> 00:01:42,630
is that we're going to use a sound effect
object for each of our sound effects.

25
00:01:42,630 --> 00:01:49,260
So let's go to the place where we create
our menu button.

26
00:01:49,260 --> 00:01:51,040
So I've changed over to the menu button
class.

27
00:01:53,020 --> 00:01:57,370
And we'll see that the menu button field,
I've now, the menu button class

28
00:01:57,370 --> 00:02:02,310
now has a field called button click, and
it is the data type sound effect.

29
00:02:02,310 --> 00:02:04,010
So I'm going to uncomment that.

30
00:02:06,270 --> 00:02:09,580
And I'm also going to come down here to
the constructor for the Menu

31
00:02:09,580 --> 00:02:16,210
button, and show you that what happens is
we actually need to load content.

32
00:02:17,530 --> 00:02:19,100
And it's a sound effect this time.

33
00:02:19,100 --> 00:02:22,870
It's not a Texture2d.
It's a different data type.

34
00:02:22,870 --> 00:02:24,890
This time, it's a sound effect.

35
00:02:24,890 --> 00:02:28,630
And we give it the name of the sound
effect we're going to use.

36
00:02:28,630 --> 00:02:32,100
Now if you look over here in the content
area

37
00:02:32,100 --> 00:02:36,800
My sound effects are just WAV files that
I've added as content.

38
00:02:36,800 --> 00:02:39,540
We no longer have an exact project that we
add as content.

39
00:02:39,540 --> 00:02:45,330
We actually add the RAW WAV files as
content to our project instead.

40
00:02:46,710 --> 00:02:50,290
Okay, so now, because I have this asset
called button

41
00:02:50,290 --> 00:02:53,580
click, as you can see over here with its
name.

42
00:02:53,580 --> 00:02:57,630
I can load that asset just like I've been
loading texture 2Ds,

43
00:02:57,630 --> 00:03:02,210
but this time it's going to load it as a
sound effect, which is a WAV file.

44
00:03:03,380 --> 00:03:07,490
So, now if I go one more thing I need to
change before we actually get a click.

45
00:03:08,820 --> 00:03:15,210
If we go over here and we say, when they
have pressed the left mouse button on top

46
00:03:15,210 --> 00:03:18,530
of this menu button, take the button click
sound

47
00:03:18,530 --> 00:03:23,180
effect, which I just created the object
for in

48
00:03:23,180 --> 00:03:28,404
my constructor, and play it.
So if I f5 at this point, I

49
00:03:28,404 --> 00:03:33,970
get a click when I click on the play
button, or on the grid button.

50
00:03:33,970 --> 00:03:38,200
I'll click on the grid button, and as you
can see we get a click.

51
00:03:39,300 --> 00:03:42,120
So the next thing that I'm going to do, is
I

52
00:03:42,120 --> 00:03:48,350
will add the destroy sound effect because
that's the next easier thing

53
00:03:48,350 --> 00:03:50,710
to do.
So back in our game 1 class.

54
00:03:52,650 --> 00:03:55,780
Just like I needed a sound effect for the
button click, I

55
00:03:55,780 --> 00:03:59,750
need a sound effect for destroy, so I'll
uncomment that sound effect.

56
00:04:02,110 --> 00:04:05,380
And the next thing I need to do is
actually

57
00:04:05,380 --> 00:04:09,160
load its content, so I'll come down here
to load content.

58
00:04:09,160 --> 00:04:13,200
And this particular sound effect was
called Zombie.

59
00:04:13,200 --> 00:04:16,880
That was the name of the WAV file, so
that's the name of the asset.

60
00:04:18,020 --> 00:04:21,320
And this is unfortunately one of those
areas where exact is

61
00:04:21,320 --> 00:04:25,820
really useful, because you don't care what
the WAV files are named.

62
00:04:25,820 --> 00:04:27,320
You just use cue names

63
00:04:27,320 --> 00:04:30,190
when you're using the exact audio assets,
but we don't

64
00:04:30,190 --> 00:04:33,840
get that capability here, so that's just
the way it is.

65
00:04:33,840 --> 00:04:37,190
And so we'll use the Zombie WAV file and
of course you could

66
00:04:37,190 --> 00:04:40,670
change the WAV file name to destroy as
well, if you wanted to.

67
00:04:40,670 --> 00:04:44,580
And then the last thing we need to do, is
we need to make

68
00:04:44,580 --> 00:04:50,610
it so that the zombie sound effect plays
when the fish eats the bear.

69
00:04:50,610 --> 00:04:52,169
So recall that was down in Update.

70
00:04:54,278 --> 00:04:59,620
The line two, it was down in the check and
resolve fish bear collisions.

71
00:05:00,950 --> 00:05:05,599
And so here's where the fish ate the bear,
so we're going to play that sound.

72
00:05:07,470 --> 00:05:11,384
And now, when I f5, this time of course,
I'll pick play.

73
00:05:11,384 --> 00:05:11,699
[SOUND]

74
00:05:11,699 --> 00:05:16,460
As I drive around eating the teddy bears,
you see that it plays that destroy sound.

75
00:05:16,460 --> 00:05:18,080
We still don't get a bounce sound.

76
00:05:18,080 --> 00:05:19,430
I can dribble a teddy bear.

77
00:05:19,430 --> 00:05:24,060
We still don't get a bounce sound, because
we haven't played to that one yet.

78
00:05:24,060 --> 00:05:25,970
So, let's actually do that one next

79
00:05:25,970 --> 00:05:29,690
because again, we have a little extra
complication.

80
00:05:29,690 --> 00:05:34,310
Remember the way we implemented our exact
project, was

81
00:05:34,310 --> 00:05:36,760
we set it up so that the bounce sound
randomly

82
00:05:36,760 --> 00:05:41,078
picked between one of two sound effects,
or cues.

83
00:05:41,078 --> 00:05:42,040
no sorry.

84
00:05:42,040 --> 00:05:44,980
Between two sounds in the sound bank.

85
00:05:44,980 --> 00:05:49,130
For a single cue, and it randomly picked
between them.

86
00:05:49,130 --> 00:05:52,750
Well we don't get that capability without
using

87
00:05:52,750 --> 00:05:55,660
exact, so we need to implement it
ourselves.

88
00:05:55,660 --> 00:05:56,600
But we can.

89
00:05:56,600 --> 00:05:59,720
We understand how to randomly pick between
a few things.

90
00:05:59,720 --> 00:06:02,090
We did this way back when we were randomly

91
00:06:02,090 --> 00:06:05,150
picking a character to display when we
clicked on

92
00:06:05,150 --> 00:06:09,200
characters with the mouse, back in the XNA
input module.

93
00:06:09,200 --> 00:06:11,280
So, we know we can do this.

94
00:06:11,280 --> 00:06:12,580
It's just a little extra work for us.

95
00:06:14,210 --> 00:06:18,260
So, this time, I've declared a variable
called bounce, but this

96
00:06:18,260 --> 00:06:22,400
is a two dimensional array of sound
effects, so sound effect there.

97
00:06:23,400 --> 00:06:27,140
Sorry, it's a two element, one dimensional
array

98
00:06:27,140 --> 00:06:28,730
of sound effects.

99
00:06:28,730 --> 00:06:32,720
And so we're going to basically put a
couple of

100
00:06:32,720 --> 00:06:36,130
sound effects into this array, and then
randomly pick between them.

101
00:06:36,130 --> 00:06:38,700
And that's why I have this random number
generator here as well.

102
00:06:39,770 --> 00:06:45,450
Back to load content so that we can load
the content for those sound effects.

103
00:06:46,780 --> 00:06:49,200
So, bounce zero, the zero element to

104
00:06:49,200 --> 00:06:52,300
the array get's the confetti sound effect
loaded

105
00:06:52,300 --> 00:06:54,890
into it by the content manager.

106
00:06:54,890 --> 00:06:59,220
And the, the first, the oneth element of
the

107
00:06:59,220 --> 00:07:02,130
array gets the pirate sound effect loaded
into it.

108
00:07:02,130 --> 00:07:05,560
So at this point we now have the bounce
array that contains

109
00:07:05,560 --> 00:07:10,899
two sound effects, and again we're at
check and resolve fish bear collision.

110
00:07:14,190 --> 00:07:16,010
And this time we played the bouncing

111
00:07:16,010 --> 00:07:20,410
sound, but notice that I've added this
part.

112
00:07:20,410 --> 00:07:24,540
So I'm going to randomly going to pick a
number that is either 0 or 1.

113
00:07:24,540 --> 00:07:31,140
And use that as the index into the array
to play that particular sound effect.

114
00:07:31,140 --> 00:07:34,220
And so that way I get that same random
sound

115
00:07:34,220 --> 00:07:37,630
effect getting played on a bounce behavior
that I want.

116
00:07:37,630 --> 00:07:40,460
And now we can f5 again.

117
00:07:40,460 --> 00:07:43,048
And I'll play, and I'll make sure

118
00:07:43,048 --> 00:07:43,722
[SOUND]

119
00:07:43,722 --> 00:07:47,750
I'm bouncing and as you can see we're
getting some random

120
00:07:47,750 --> 00:07:51,270
bouncing sound effects each time the teddy
bear hits the fish.

121
00:07:54,680 --> 00:08:00,138
So, that's it for the sound effects that
we have designed and implemented,

122
00:08:00,138 --> 00:08:00,659
[LAUGH]

123
00:08:00,659 --> 00:08:04,150
I guess.
So we button click on menu buttons.

124
00:08:04,150 --> 00:08:06,710
We play the zombie sound when the fish

125
00:08:06,710 --> 00:08:08,970
eats the bear, and we have the bouncing
sound.

126
00:08:08,970 --> 00:08:12,030
The last thing that we need is our
background music.

127
00:08:12,030 --> 00:08:13,560
And I said I was putting that off

128
00:08:13,560 --> 00:08:16,590
til last, because it's a little more
complicated.

129
00:08:16,590 --> 00:08:20,370
It's not hugely complicated, it's just a
little more complicated.

130
00:08:20,370 --> 00:08:22,620
And here's what adds the complication.

131
00:08:22,620 --> 00:08:25,870
The complication is that we have no
capability to make

132
00:08:25,870 --> 00:08:27,830
sound effects loop.

133
00:08:27,830 --> 00:08:30,130
And, what we want for our background music
is, we

134
00:08:30,130 --> 00:08:33,450
want it to loop while the player is
playing the game.

135
00:08:33,450 --> 00:08:37,160
So, instead of using a sound effect, I

136
00:08:37,160 --> 00:08:40,200
need to use something called a sound
effect instance.

137
00:08:41,280 --> 00:08:44,430
Let's go to the load content method and
see how we handle that.

138
00:08:47,340 --> 00:08:49,090
So I'll uncomment these four lines of
code.

139
00:08:50,650 --> 00:08:52,890
The first thing we do is we actually load
a sound

140
00:08:52,890 --> 00:08:56,860
effect, because a sound effect instance
starts from a sound effect.

141
00:08:57,930 --> 00:09:00,230
So, this line of code is just like

142
00:09:00,230 --> 00:09:02,540
the other sound effect loading things
we've done.

143
00:09:04,190 --> 00:09:07,720
The next thing we do though, is we take
that sound effect,

144
00:09:07,720 --> 00:09:12,770
which is an object, and we call the create
instance method on it.

145
00:09:12,770 --> 00:09:17,010
And what that does is it creates and
returns a sound

146
00:09:17,010 --> 00:09:22,190
effect instance object, which we're
putting into the background music field.

147
00:09:22,190 --> 00:09:27,110
Once we've done that, we can access the Is
Looped property

148
00:09:27,110 --> 00:09:31,860
of this sound effect instance object, and
set it to true.

149
00:09:31,860 --> 00:09:33,409
So now we've made it loop.

150
00:09:34,430 --> 00:09:37,830
And then we play the background music just
like we played the background

151
00:09:37,830 --> 00:09:40,540
music, or just like we've played sound
effects.

152
00:09:40,540 --> 00:09:43,080
This time we're playing a sound effect
instance, but we

153
00:09:43,080 --> 00:09:46,940
just call the play method, and the
background music started playing.

154
00:09:46,940 --> 00:09:49,649
But because we set this Is Looped
property, this,

155
00:09:49,649 --> 00:09:53,023
the background music will loop while we're
playing the game.

156
00:09:53,023 --> 00:09:53,532
[MUSIC]

157
00:09:53,532 --> 00:09:55,896
So, I'll play it one more time.

158
00:09:55,896 --> 00:09:57,840
And now we see that we actually do

159
00:09:57,840 --> 00:10:02,280
have the sound effect playing, and that's
great news.

160
00:10:02,280 --> 00:10:06,050
So, in this lecture, that's it for the
code.

161
00:10:06,050 --> 00:10:08,300
We've added the sound effects just like we
did in the

162
00:10:08,300 --> 00:10:12,360
previous lecture, except we did it without
using an exact project.

163
00:10:12,360 --> 00:10:16,670
We did it using the sound effect and sound
effect instance classes.

164
00:10:16,670 --> 00:10:18,690
And that's what you need to do, at least

165
00:10:18,690 --> 00:10:23,585
at the time of the recording of this video
for, if you're using MonoGame.

166
00:10:24,760 --> 00:10:27,870
And so that's it for this module as well.

167
00:10:27,870 --> 00:10:30,860
We learned how to, if you're using exact,
how to create

168
00:10:30,860 --> 00:10:33,980
an exact project and integrate those audio
assets into your game.

169
00:10:33,980 --> 00:10:38,310
And if you're using MonoGame, we learned
how you could just use WAV files

170
00:10:38,310 --> 00:10:40,120
and sound effect and sound effect
instances

171
00:10:40,120 --> 00:10:43,600
to add music and audio to your game.

172
00:10:43,600 --> 00:10:47,810
Next time, we're actually going to talk
about how we can

173
00:10:47,810 --> 00:10:52,302
use keyboard input and provide text output
in our XNA games.