Playing audio in game background

Hello everyone. I have small problem, i need to play audio in my game background while playing game and i cant do that. I used Google, but i cant find anything useful. I don’t care about audio format. Please help me.

Are you using Slick2D to build your game? Also, what sort of music are you looking for?

I am using LWJGL. I have music in wav, mp3, ogg… I don’t really care about format.

I don’t deem myself qualified for LWJGL specific advice, but I will say that .wav’s are to be avoided, used a compressed data format, preferably a non-patent encumbered one. (.ogg’s are good)

A standard CD-quality wav file takes up over a megabyte for every 6 seconds of audio, massively bloating the game download. A decent quality (160kbps, most won’t know the difference) ogg on the other hand is almost 9 times smaller for a given length audio track.

Also, since the audio has to be uncompressed anyway when played (meaning it takes up the full Mb per 6 seconds), for long tracks (background music) you should look into streaming the data from the disk, so that only a small portion is in RAM at any given time.

I think Slick2D comes with a OGG decoder, but LWJGL is (generally) just for the graphics.

So what you would use is a OGG decoder like JOrbis: http://www.jcraft.com/jorbis/
A really good, in depth tutorial for beginners: http://www.jcraft.com/jorbis/tutorial/Tutorial.html

Slick2D music is extremely easy to setup but I think it only does support ogg, at least from what I have seen. Sorry I can’t help you OP :frowning:

If you are using any other format other than ogg, use a converter. I think ogg is one of the best for games, especially because there is no other decoder for Java :point:.

Ogg is great for many reasons, but it being the only thing available isn’t one of them. Try a quick google around.

Speaking of Google: this should get you well on your way, OP

Yup. OGG is ultra-easy in slick2d. It even keeps the music playing through different game states and everything.

Simply:

public class YourState extends BasicGameState{
	private Music music;
	
	YourState(int state){
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
		//Load up the track you want to play.
		music = new Music("res/yourSong.ogg");
	}
	
	public void enter(GameContainer container, StateBasedGame sbg) throws SlickException{
		//Start the music loop when you first enter the state, will not end until you use music.stop() or .pause() somewhere, even if you change states.
		music.loop();
	}	

LWJGL comes with OpenAL. You can look on the wiki under the OpenAL section for some tutorials to get you started. From the tutorial:

AL10.alSourcePlay(source);

This is all you would need to play the sound (after loading and buffering of course). You can stop or pause it at any time.

You can also use paulscode 3D sound system to easily play sounds in 3D space (though it can also be used for 2D games too!) As noted by the previous posters, .wav is an uncompressed, lossless format and as a result is very large. .ogg would be a better option.

Thanks for help! I will try everything soon.

Everything worked! I used slick2d. Thanks again!