-
Notifications
You must be signed in to change notification settings - Fork 5
Loading Unloading Sounds
The following file formats are supported: wav, flac, ogg, mp3, aiff, qoa (see the full list).
There is a loader for each format and a SoundBufferLoader that works with libGDX's AssetManager.
This loading mode will block the main thread until it's done.
SoundBuffer wavSound = SoundLoader.load(Gdx.files.internal("sound.wav"));
SoundBuffer aiffSound = SoundLoader.load(Gdx.files.internal("sound.aiff"));
SoundBuffer oggSound = SoundLoader.load(Gdx.files.internal("sound.ogg"));
SoundBuffer flacSound = SoundLoader.load(Gdx.files.internal("sound.flac"));
SoundBuffer mp3Sound = SoundLoader.load(Gdx.files.internal("sound.mp3"));
SoundBuffer qoaSound = SoundLoader.load(Gdx.files.internal("sound.qoa"));
// if you want to read the samples in code
ReadableSoundBuffer readableSound = SoundLoader.load(Gdx.files.internal("sound.wav"));
// or if you want a reversed version of the sound
SoundBuffer reversedSound = SoundLoader.loadReverse(Gdx.files.internal("sound.wav"));Make sure to dispose sounds when you're done using them.
sound.dispose();It's a bit different for streamed sounds. There's no SoundBuffer you can put into a StreamedSoundSource like it's done with SoundBuffer and BufferedSoundSources. Instead you create a StreamedSoundSource with a FileHandle directly, so the source comes bundled with it's audio data.
StreamedSoundSource wavSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.wav"));
StreamedSoundSource aiffSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.aiff"));
StreamedSoundSource oggSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.ogg"));
StreamedSoundSource flacSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.flac"));
StreamedSoundSource mp3SoundSource = new StreamedSoundSource(Gdx.files.internal("sound.mp3"));
StreamedSoundSource qoaSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.qoa"));A StreamedSoundSource must be disposed as well, if you don't need it anymore:
source.dispose();In case you don't know how to use the libGDX AssetManager, please read this wiki entry first: AssetManager
Asynchronous loading is supported only for SoundBuffer as the other sources are async by nature.
First, you need to tell the AssetManager about the loader (you can skip this step if you provided the AssetManager in AudioConfig):
audio.registerAssetManagerLoaders(assetManager);Afterwards, you can queue sound files for loading and retrieve SoundBuffers when the AssetManager finished loading:
assetManager.load("sound.wav", SoundBuffer.class);
assetManager.load("sound.aiff", SoundBuffer.class);
assetManager.load("sound.ogg", SoundBuffer.class);
assetManager.load("sound.flac", SoundBuffer.class);
assetManager.load("sound.mp3", SoundBuffer.class);
assetManager.load("sound.qoa", SoundBuffer.class);
// ...
SoundBuffer wavSound = assetManager.get("sound.wav", SoundBuffer.class);
SoundBuffer aiffSound = assetManager.get("sound.aiff", SoundBuffer.class);
SoundBuffer oggSound = assetManager.get("sound.ogg", SoundBuffer.class);
SoundBuffer flacSound = assetManager.get("sound.flac", SoundBuffer.class);
SoundBuffer mp3Sound = assetManager.get("sound.mp3", SoundBuffer.class);
SoundBuffer qoaSound = assetManager.get("sound.qoa", SoundBuffer.class);Here's a full example: AsyncLoadTest