Audio#
Sound is crucial in creating games. From background music to environment ambience to sound effects, audio in a game can make or break your player immersion.
Before proceeding, here are a few points to take note of:
- GameKit does not support streaming audio from disk/network
- GameKit only supports the wave format (.wav)
A Simple Sample#
import dev.gamekit.audio.AudioClip;
import dev.gamekit.audio.AudioGroup;
import dev.gamekit.core.Application;
import dev.gamekit.core.Audio;
import dev.gamekit.core.Input;
import dev.gamekit.core.Scene;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
public class AudioSample extends Scene {
private AudioClip clip;
public AudioSample() {
super("Main Scene");
try{
clip = Audio.loadClip("bg-music.wav");
} catch (UnsupportedAudioFileException | IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Application game = new Application("Audio Sample") { };
game.loadScene(new AudioSample());
game.run();
}
@Override
protected void update() {
if (Input.isKeyDown(Input.KEY_SPACE)) {
if (!clip.isPlaying()) {
clip.play();
} else {
clip.stop();
}
}
}
}
What we have done#
- We use the
Audioutility to load an audio resource file named "bg-music.wav". - In the
updatelifecycle method, we start/stop playback of the audio clip when the space bar is pressed. - In the static
mainmethod, we created anApplicationinstance with title, "Audio Sample", loaded an instance of our scene subclass and called therunmethod to start the application.
GameKit audio is a bit of a broad topic and has been categorized in the sections below: