In this video tutorial we look at all aspects of audio programming in LibGDX. We look at how to play back sound effects, stream music audio we even record audio from the microphone and play it back. We also touch on a couple important concepts like cross platform file formats and creating transitions between music tracks.
You can see the video in full 1080p resolution here. All of the code used in this video is presented below. For more LibGDX video tutorials, go here.
Sound Effects
Playing a Sound Effect
ackage com.gamefromscratch; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.utils.Timer; public class SoundFXDemo extends ApplicationAdapter { Sound sound; @Override public void create () { sound = Gdx.audio.newSound(Gdx.files.internal("glassBreak.wav")); sound.play(); } @Override public void render () { } @Override public void dispose() { sound.dispose(); } }
Setting Volume
sound = Gdx.audio.newSound(Gdx.files.internal("glassBreak.wav")); long id = sound.play(1.0f); sound.setVolume(id,1.0f);
Setting Pitch
sound = Gdx.audio.newSound(Gdx.files.internal("glassBreak.wav")); long id = sound.play(); sound.setPitch(id,0.2f);
Setting Pan
// Track must be in mono format to support panning sound = Gdx.audio.newSound(Gdx.files.internal("glassBreakMono.wav")); long id = sound.play(); sound.setPan(id,1.0f,1f);
Setting Volume, Pitch and Pan all at once
sound = Gdx.audio.newSound(Gdx.files.internal("glassBreakMono.wav")); sound.play(1.0f,0.0f,0.8f);
Looping and Pausing
sound = Gdx.audio.newSound(Gdx.files.internal("glassBreak.wav")); sound.loop(); // Wait 10 seconds, then pause playback Timer.schedule(new Timer.Task(){ @Override public void run(){ sound.pause(); } }, 10);
Multiple Concurrent Sounds
sound = Gdx.audio.newSound(Gdx.files.internal("glassBreakMono.wav")); // Play 2, one fast, one slow final long fastSound = sound.loop(1f,2.5f,0f); final long slowSound = sound.loop(1f,0.5f,0f); // Wait 5 seconds, then stop ALL Timer.schedule(new Timer.Task() { @Override public void run() { //sound.stop(); // Or stop just one sound.stop(fastSound); } }, 5);
Music
package com.gamefromscratch; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.utils.Timer; public class MusicDemo extends ApplicationAdapter { Music song1,song2; @Override public void create () { song1 = Gdx.audio.newMusic(Gdx.files.internal("song1.mp3")); song2 = Gdx.audio.newMusic(Gdx.files.internal("song2.mp3")); song1.play(); // Then set it so song 2 plays when song1 ends song1.setOnCompletionListener(new Music.OnCompletionListener() { @Override public void onCompletion(Music music) { song2.play(); } }); // You can pan and set volume like sound fx, but cant play multiple instances and cant set pitch // You can however get the position of the sound ( but not set it ) // Let's do a timer that lower the volume over the last 5 seconds of the song // Built in options are pretty limited, so there is no way to poll for the length of a song // Without using the audio-extension Timer.schedule(new Timer.Task() { @Override public void run() { if(song1.isPlaying()) if(song1.getPosition() >= 10) song1.setVolume(song1.getVolume() - 0.2f); } },10,1,4); } @Override public void render () { } @Override public void dispose() { song1.dispose(); song2.dispose(); } }
Audio Recording and Playback
package com.gamefromscratch; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.AudioDevice; import com.badlogic.gdx.audio.AudioRecorder; public class AudioRecorderDemo extends ApplicationAdapter { @Override public void create () { // This code may not actually work during video recording due to Microphone being in use. AudioRecorder recorder = Gdx.audio.newAudioRecorder(44100,true); short[] audioBuffer = new short[44100 * 5]; // read() will fail if device doesnt support given resolution or a mic is not attached recorder.read(audioBuffer,0,audioBuffer.length); AudioDevice audioDevice = Gdx.audio.newAudioDevice(44100,true); audioDevice.writeSamples(audioBuffer,0,audioBuffer.length); audioDevice.dispose(); recorder.dispose(); Gdx.app.exit(); } @Override public void render () { } }