Sound effects are small audio samples, usually no longer than a few seconds, that are played back on specific game events such as a character jumping or shooting a gun. Sound effects can be stored in various formats. libGDX supports MP3, OGG and WAV files. RoboVM (iOS) currently does not support OGG files. ** On Android, a Sound instance can not be over 1mb in size (uncompressed raw PCM size, not the file size). If you have a bigger file, use Music Sound interface. Loading a sound effect works as follows:
Sound sound = Gdx.audio.newSound(Gdx.files.internal("data/mysound.mp3"));
"mysound.mp3" from the internal directory data.
Once we have the sound loaded we can play it back:
sound.play(1.0f);
Sound instance can be called many times in a row, e.g. for a sequence of shots in a game, and will be overlaid accordingly.
Sound.play() returns a long which identifies that sound instance. Using this handle we can modify that specific playback instance:
long id = sound.play(1.0f); // play new sound and keep handle for further manipulationsound.stop(id); // stops the sound instance immediatelysound.setPitch(id, 2); // increases the pitch to 2x the original pitchid = sound.play(1.0f); // plays the sound a second time, this is treated as a different instancesound.setPan(id, -1, 1); // sets the pan of the sound to the left side at full volumesound.setLooping(id, true); // keeps the sound loopingsound.stop(id); // stops the looping sound
GwtApplicationConfiguration.preferFlash = true
Once you no longer need a Sound, make sure you dispose of it:
sound.dispose();
Accessing the sound after you disposed of it will result in undefined errors.
