Sound and Music

From Slick2D Wiki
Jump to: navigation, search

Sounds and music are available in a similar manner to Images. You simple construct an object of the appropriate type (either Sound or Music) with a reference to the resource to be loaded. Sound and music are seperated due to their use of source (the term OpenAL uses for channels). When a sound effect a played the next available source is selected and used to play the sound. If no source is available the sound isn't played. However, music is always played on a single reserved channel which is not interferred with by the sound effects.

Sound FX[edit]

The sound class supports loading WAVs and OGGs. Sound effects can be played, stopped and looped (useful for engine sounds). Note that if no channel is available when attempting to play a sound it will not be heard. Playing a sound in Slick is as simple as:

Sound fx = new Sound("res/boom.wav");
fx.play();
// or fx.play(1.0f,0.5f) to set the volume and pitch of the sound effect

You can also play sound effects using 3D sound to make them sound as thought they are originating from a particular location. You can do this by using playAt():

Sound fx = new Sound("res/bounds.wav");
fx.playAt(-1,0,0);

Music[edit]

Music in Slick is treated in much the same manner as Sound FX with the notable exception mentioned above. Slick supports WAV, OGG and MOD/XM music tracks. Note that if you request that a music track be played or looped any music currently playing will be stopped as a side effect.

Music can be loaded a and playing (repeatedly) like so:

Music music = new Music("res/musicwithrocksin.ogg");
music.loop();

Troubleshooting[edit]

On some systems, playback of OGG files may be choppy, crackly or otherwise of poor quality. If you are hearing frequent popping and clicks in playback, it may be because you do not have the proper OpenAL drivers installed on your system.