This article is about Game Development
How to add Sound to Game Using Java
By NIIT Editorial
Published on 23/08/2021
Just like every other multimedia application, sound is very important in a game. Every action in a game has a corresponding sound that makes it interesting and appealing to the users. This article will focus on adding sound to games like the background music, the sound of bouncing of a ball and the sound of “GAME OVER” in a funny voice at the end of the game.
Creating a sound
This is a very easy process. You just need to look up a “free audio editor” on Google and start creating your own sounds. The following is an example of a Free audio editor:
With the help of this audio editor, we have created the following archives: back.wav, gameover.wav and ball.wav. You can also create such sounds by watching tutorial videos on YouTube. The above three archives can be downloaded and have to be copied to the com.AudioLib.minitennis7 package.
Play sounds using AudioClip
These sound archives can be played using the AudioClip class. The user can create AudioClip using the following static method - Applet.newAudioClip(URL url) of the Applet class. This static method requires an URL object that indicates where an audio archive is placed that the user wants to load and play.
The following fragment uses a directory inside the local archive system:
The user can look for the archives using the classpath. This technique uses java for loading the classes or specifically the *.class archives that define the classes of the program. If the user wants to obtain an URL from the classpath, he can use the getResource(String name) method of the Class class, wherein “name” is the name of the archive he wants to obtain.
Given below are two ways by which the user can obtain the URL of the "back.wav" archive. This archive is located in the same location as the SoundTest.class archive.
In the above fragment, both "SoundTest.class" and "new SoundTest().getClass()" provide a class object which contains the getResource method the user had intended to use.
Below is an example showing how the SoundTest class is created to depict the working of AudioClip:
The above fragment explains the way to obtain the back.wav archive from the classpath. Basically, the classpath is the directories and archives *.jar collection from where the program can read the classes (*.class archives).
The benefit of using this method is that the programmer will only have to specify the position of the archive concerning the class that uses it. In the example given above, since it is in the same package, only "back.wav" is supposed to be written.
One more advantage of using this method is that the sound archives can be incorporated in the *.jar archive. As soon as an URL object is obtained, an AudioClip object can be created using the Applet.newAudioClip(url).
In the above fragment, the AudioClip object contains a play() method, which initiates an independent thread. This thread plays the audio of the archive but just once. If you want to play the audio more than once, the loop() method of AudioClip can be used as it is capable of playing the sounds repeatedly until the stop() method is called over for the same AudioClip.
As it has been mentioned, to keep the AudioClip in a game, a SoundClass is to be created. This will be constant with an AudioClip for each and every sound used in the game. Furthermore, all these constants are public, which means that any object that has access to them can play them. To understand game development better check out the Game Development Course offered by NIIT.
Game Development Course
Becoming a Game Developer: A Course for Programmers. Design and Build Your Own Game using Unity.
Job Placement Assistance
Course duration: 20 weeks