1
00:00:01,380 --> 00:00:06,120
In this lecture we're gonna go through the process of playing sound effects and music in our game.

2
00:00:06,390 --> 00:00:10,770
Before doing anything we need to have some audio files to use in our project.

3
00:00:10,770 --> 00:00:17,180
You can use any sound files that you have whether it's MP 3 or that wave but you don't have any available.

4
00:00:17,340 --> 00:00:24,110
I created these two sound files a while back and I'm including them as part of this lecture.

5
00:00:24,210 --> 00:00:27,360
We have flipped out wave and nature that Argh.

6
00:00:27,390 --> 00:00:33,390
This is what that wave sounds like in nature.

7
00:00:33,390 --> 00:00:35,060
The ARG is a music track.

8
00:00:35,060 --> 00:00:35,670
Sounds like this

9
00:00:46,920 --> 00:00:52,290
once you have your files ready we can go into Visual Studio and your project and go to the content folder

10
00:00:52,440 --> 00:00:54,360
and we're going to create a new folder

11
00:00:58,900 --> 00:01:04,520
for sounds okay.

12
00:01:04,800 --> 00:01:12,990
Now when you actually put those files we talked about earlier into this folder so I'm going to copy

13
00:01:12,990 --> 00:01:15,410
these.

14
00:01:15,420 --> 00:01:23,820
Then I'm going to go to the game project file and we're going to find our content folder.

15
00:01:23,910 --> 00:01:30,560
We have our new sounds folder that we created and was going to paste those two files in there.

16
00:01:30,570 --> 00:01:36,000
Next we're going to open up our content that MDG CV or the content pipeline

17
00:01:40,140 --> 00:01:47,100
we want to go over here to add existing folder These are sounds folder

18
00:01:50,330 --> 00:01:54,910
okay and you can see a two full are are two files are in there already.

19
00:01:54,910 --> 00:02:00,070
Now that they're in there we can do A build actually we should save.

20
00:02:00,070 --> 00:02:01,120
And then we can do A build

21
00:02:05,530 --> 00:02:11,360
and as long as you see that it's building sound slash blip that wave and building sounds slash nature

22
00:02:11,360 --> 00:02:12,120
that ARG.

23
00:02:12,380 --> 00:02:18,970
Then you know that it's fine because these two have built now they're ready to use in our game.

24
00:02:19,000 --> 00:02:25,030
So in game one let's see yes we need to load the sounds into our code.

25
00:02:25,120 --> 00:02:28,320
This is really similar to loading any other asset into our code.

26
00:02:28,600 --> 00:02:34,690
Before we do anything though we need to actually include a new package actually going to put it up here

27
00:02:36,850 --> 00:02:44,550
using Microsoft that X and A that framework that audio.

28
00:02:44,680 --> 00:02:52,240
So this package allows us to use sound effects which is what blip that wave is now to love the sounds.

29
00:02:52,360 --> 00:02:57,370
We're going to load blip that wave first and here's something we need to keep in mind we only need to

30
00:02:57,370 --> 00:03:03,100
load the sound once even though we might be using sound effect multiple times say for blip that way.

31
00:03:03,160 --> 00:03:08,260
Let's let's use blip that wave whenever the player shoots a projectile even though they're shooting

32
00:03:08,260 --> 00:03:09,790
lots projectiles.

33
00:03:09,790 --> 00:03:11,910
We only need one reference to the sound file.

34
00:03:11,920 --> 00:03:17,420
We don't need to load it every single time the projectile fires.

35
00:03:17,490 --> 00:03:21,090
We want these sounds to be available throughout all the classes.

36
00:03:21,090 --> 00:03:30,700
So we're going to do is create a static class for this public static class.

37
00:03:30,960 --> 00:03:32,490
We'll call it my sounds

38
00:03:40,160 --> 00:03:53,430
and we'll start off with public static sound effect is the type and we call this projectile sound and

39
00:03:53,430 --> 00:03:55,600
that's our declaration.

40
00:03:55,990 --> 00:04:01,440
This is a nice trick for when you need some data that's available across all your classes or all of

41
00:04:01,440 --> 00:04:02,320
your files.

42
00:04:02,500 --> 00:04:08,530
Because now we just add to say my sounds that protect our sound no matter what file we're in we'll be

43
00:04:08,530 --> 00:04:11,060
able to access it.

44
00:04:11,070 --> 00:04:22,050
Now let's go down and actually load it will load it right here I guess we just say my sounds that projectile

45
00:04:22,050 --> 00:04:33,540
sound equals content that load the tape is sound effect and then we need to give it a path

46
00:04:36,630 --> 00:04:37,500
which is

47
00:04:39,990 --> 00:04:41,190
sounds.

48
00:04:41,580 --> 00:04:43,090
Slash bullet.

49
00:04:45,490 --> 00:04:50,530
And remember you do not put the extension you just say blip not blip that wave.

50
00:04:50,530 --> 00:04:52,740
So now our Sound Effect is loaded into the game.

51
00:04:52,930 --> 00:04:55,540
But we need to actually play it at some point.

52
00:04:55,630 --> 00:05:01,790
And like I mentioned earlier I want this to play whenever the player shoots a projectile.

53
00:05:01,990 --> 00:05:04,150
So let's go in to play that C.S.

54
00:05:07,440 --> 00:05:09,890
and they shoot the projectile right here.

55
00:05:09,890 --> 00:05:24,010
So right afterwards I'm going to do my sounds that projectile sound that play just like that.

56
00:05:24,120 --> 00:05:26,390
This line will play the sound.

57
00:05:26,500 --> 00:05:28,080
And now at this point we're ready to test it

58
00:05:36,580 --> 00:05:37,130
and it works

59
00:05:42,250 --> 00:05:48,040
now you should know that this play function was play method actually has some optional parameters that

60
00:05:48,040 --> 00:05:49,190
we can put in there.

61
00:05:49,300 --> 00:05:52,660
So we'll do this for example one F

62
00:05:56,410 --> 00:06:04,240
zero point five F and zero f remember that the f just clarifies that this is a float.

63
00:06:04,270 --> 00:06:10,260
Now what these parameters actually represent This value represents the volume of the sound effect.

64
00:06:10,420 --> 00:06:13,150
This one represents the pitch for the sound effect.

65
00:06:13,270 --> 00:06:14,950
And this one represents the pan.

66
00:06:15,730 --> 00:06:23,800
So to clarify this is a zero to one value zero being completely quiet and one being full volume same

67
00:06:23,800 --> 00:06:27,220
volume as the sound file itself.

68
00:06:27,250 --> 00:06:28,720
This is the pitch.

69
00:06:28,750 --> 00:06:31,690
So here I signified zero point five f.

70
00:06:31,870 --> 00:06:36,340
That means that the pitch is going to raise by half an octave.

71
00:06:36,340 --> 00:06:38,740
Every one is an octave.

72
00:06:38,740 --> 00:06:45,750
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

73
00:06:46,360 --> 00:06:51,170
if I did negative to f that would mean the pitch drops by two octaves.

74
00:06:51,370 --> 00:06:58,480
I'm actually going to change it back to zero point five and then pan pandas refers to the focus of the

75
00:06:58,480 --> 00:07:00,400
left and right speaker.

76
00:07:00,400 --> 00:07:05,740
And this is a negative one value to one value if it's negative one it's all the way focus on the left

77
00:07:05,740 --> 00:07:11,500
speaker if it's positive one is all the way focus on the right speaker so having it at zero would make

78
00:07:11,500 --> 00:07:20,540
it equal in both so if this changes if I save and start the only thing that changes the pitch

79
00:07:23,950 --> 00:07:25,940
the pitch of each shot is slightly higher now

80
00:07:31,010 --> 00:07:35,000
using the standard sound effect type is probably all you're going to need for your games.

81
00:07:35,540 --> 00:07:38,160
However if you need further control of your sounds.

82
00:07:38,290 --> 00:07:41,080
Look up these sound effect instance class.

83
00:07:41,240 --> 00:07:44,110
It's similar buffer is a bit more control if you need it.

84
00:07:45,650 --> 00:07:49,710
Next we're gonna get our background music playing which is a slightly different process.

85
00:07:49,800 --> 00:07:52,000
We're gonna go back to Game 1 and see us.

86
00:07:52,280 --> 00:07:56,160
And actually since it's a song it operates a little bit differently.

87
00:07:56,450 --> 00:08:03,260
We're going to also include a new package and this one is called Microsoft that X made up framework

88
00:08:04,310 --> 00:08:11,350
dot media MEAA.

89
00:08:11,540 --> 00:08:19,070
Then we can declare our song public static song.

90
00:08:19,070 --> 00:08:23,430
We'll call it BMG music.

91
00:08:23,660 --> 00:08:26,240
And now to load it we'll go down

92
00:08:29,610 --> 00:08:32,560
to here.

93
00:08:34,440 --> 00:08:46,680
We'll do my sounds that B G music equals content that load the type of song and same deal we specify

94
00:08:46,680 --> 00:08:48,690
the path sounds.

95
00:08:48,690 --> 00:08:51,710
Slash nature.

96
00:08:54,740 --> 00:09:02,450
Now you might be wondering why is nature to OG considered to be a song while lip that wave is considered

97
00:09:02,450 --> 00:09:03,740
to be a sound effect.

98
00:09:03,840 --> 00:09:08,660
And this is actually because of the file type that waves are by default considered sound effects and

99
00:09:08,890 --> 00:09:14,930
which are considered songs but this is completely customizable within the content pipeline.

100
00:09:15,170 --> 00:09:25,370
If we click on one of these audio files we can see right down here what is being signified as our processor

101
00:09:25,820 --> 00:09:28,580
so blip is being considered a sound effect.

102
00:09:28,580 --> 00:09:35,430
While nature is considered a song but say we wanted blip to be considered a song as well we could use

103
00:09:35,430 --> 00:09:38,420
this dropdown and change it to a song.

104
00:09:38,600 --> 00:09:40,520
Then we just have to save and build again.

105
00:09:40,550 --> 00:09:48,720
And it would work as a song but we actually went as the sound effects we'll just leave it as is now

106
00:09:48,720 --> 00:09:51,220
that we have music loaded in.

107
00:09:51,420 --> 00:09:57,320
We can actually play it and for demonstration purposes I'm just gonna play it right here what you do

108
00:09:57,320 --> 00:10:07,700
is you do media player that play and then you specify the song you want to play which is my sounds that

109
00:10:07,700 --> 00:10:08,720
BMG music

110
00:10:12,530 --> 00:10:15,970
and with that are songs you play when we start up

111
00:10:19,880 --> 00:10:20,540
and it does

112
00:10:28,890 --> 00:10:36,410
media player also has some other useful methods that we can use such as media player dat stop.

113
00:10:36,490 --> 00:10:42,330
There is also media player dat pause and media player dat resume.

114
00:10:42,330 --> 00:10:44,240
You can use of those methods for different functions.

115
00:10:44,250 --> 00:10:50,760
So if you want to add the level anyone the use to stop you call stop or if maybe you pause the game

116
00:10:50,820 --> 00:10:56,130
then you could choose media player dat pause and it would pass a song and then you can resume right

117
00:10:56,130 --> 00:10:57,570
where the song left off which is pretty neat.

118
00:10:59,420 --> 00:11:03,950
And that about covers everything you need to know about sound effects and songs in modern game.


