How to Add Sound to Game Using Java

NIIT Digital
3 min readAug 23, 2021
Photo by Icons8 Team on Unsplash

Originally published at https://www.niit.com/india/

Just like every other multimedia application, the 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:

URL url = new URL(“file:/C:/Users/Eli/workspace/minitennis/src/com/AudioLib/minitennis7/back.wav”);

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.

URL url = SoundTest.class.getResource(“back.wav”);

URL url = new SoundTest().getClass().getResource(“back.wav”);

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:

package com.AudioLib.minitennis7;

import java.applet.Applet;

import java.applet.AudioClip;

import java.net.URL;

public class SoundTest {

public static void main(String[] args) throws Exception {

// System.out.println(“1”);

// URL url = new URL(

// “file:/C:/Users/Eli/workspace/minitennis/src/com/AudioLib/minitennis7/back.wav”);

// System.out.println(“2”);

// AudioClip clip = Applet.newAudioClip(url);

// System.out.println(“3”);

// clip.play();

// System.out.println(“4”);

// Thread.sleep(1000);

// URL url = new URL(

// “file:/C:/Users/Eli/workspace/minitennis/src/com/AudioLib/minitennis7/back.wav”);

URL url = SoundTest.class.getResource(“back.wav”);

AudioClip clip = Applet.newAudioClip(url);

AudioClip clip2 = Applet.newAudioClip(url);

clip.play();

Thread.sleep(1000);

clip2.loop();

Thread.sleep(20000);

clip2.stop();

System.out.println(“end”);

}

}

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).

AudioClip clip = Applet.newAudioClip(url);

AudioClip clip2 = 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.

Conclusion

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 check out the Game Development Course offered by NIIT. Also, explore NIIT’s Knowledge Centre for similar content on game development.

--

--

NIIT Digital

We are a skill development hub for learners worldwide sharing ideas on technology. Explore insightful reads at https://www.niit.com/india/knowledge-center