Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sound Music & Sound Effects.

Similar presentations


Presentation on theme: "Sound Music & Sound Effects."— Presentation transcript:

1 Sound Music & Sound Effects

2 Introduction It is hard to really understand the importance of sound in a video game until you have tried it. Imagine playing a horror game like Resident Evil without the subtle background music or environment sounds like creaking floors, etc. to help immerse you. Imagine playing Guitar Hero without sound, not much of a rhythm game anymore. Today we will learn how load, play and modify both sounds and music in our games.

3 Before we begin Dealing with both sound effects and music is the same code when it comes to our Java Game Engine. However, there are other things we should keep in mind: Sound files can only be in .wav and .mp3 formats .wav files are much bigger in size, but load much faster because they are not compressed on the computer. This means is that you should use .wav format for sound effects for speed purposes You should use .mp3 for music for space purposes Store your sound files in the appropriate sounds folder in your project, either effects or music. (Don’t forget to refresh the project in Eclipse so the will show in the package explorer)

4 Resources You can find great Sound Effects on this website: As for music, this is a grey area. Since we will not be making any money off your product and it is for educational purposes, you are free to use any music you choose. You could rip these from CDs you have at home, you can buy music online as well as any other method you know to attain music in mp3 format.

5 Loading To load a sound, all we need to do is create a new variable of type SoundClip. This is an object and requires two parameters: A file path (similar to how we loaded images) A boolean called interruptible. Use true if you want the sound to play in quick succession and they may overlap, e.g. gun shots. For example: load a sound effect named doorknock.wav that is interruptible SoundClip doorKnockSnd = new SoundClip(“/sounds/effects/doorknock.wav”, true); Note: The Snd at the end of the variable name, this is to remind us this is a sound effect. Similarly, for a music file we would use Msc instead of Snd.

6 Volume The volume of a sound is a double value measured from 0 to 1 where 1 is full volume and 0 is silent At any time you can check the volume level using the GetVolume() command. Example: Assuming we are using our doorKnockSnd sound doorKnockSnd.GetVolume() You could store, compare, or output this value if you choose. Similarly we can set the volume to any value between 0 and 1 Example: Set the volume of doorKnockSnd to 30% maximum doorKnockSnd.SetVolume(0.3); Example: Combine the two to get changes from current values. increase the volume by 5% here doorKnockSnd.SetVolume(doorKnockSnd.GetVolume() );

7 Play Sound/Music Playing sound or music is the same for both and straight forward. To achieve this goal we have two options: Play() – Play the sound effect or music once PlayLooping() – Play the sound effect repeatedly forever until manually stopped Example: Play the doorKnockSnd using both methods: doorKnockSnd.Play(); doorKnockSnd.PlayLooping(); Typically this code is called inside some if-logic in our Update. For example, you would only play a gun shot sound if some pressed the shoot button. Music is sometimes started in the LoadContent section as well depening on the desired effect of the programmer

8 Stopping Looped Sounds
To achieve this we simply need to call the Stop() command on the sound Example: doorKnockSnd.Stop(); This will also stop a non-looping sound immediately.

9 Pause/Resume You can pause and any sound by using the Pause() and Play() or PlayLooping() commands Example: Pause the doorKnockSnd sound doorKnockSnd.Pause(); To resume the sound just call the Play() or PlayLooing() command on doorKnockSnd. It is often times a good idea to track when a sound is paused using a boolean variable. Then you verify that sound is paused before your resume it rather than wasting the CPU Example: Pause and Resume if (some condition to pause) { doorKnockSnd.Pause(); pausedDoor = true; } if (some unpause condition && pausedDoor == true) { doorKnocked.Play(); pausedDoor = false; }


Download ppt "Sound Music & Sound Effects."

Similar presentations


Ads by Google