background music

Currently I’m using EasyOgg http://www.cokeandcode.com/taxonomy/term/6?from=50, for sounds aswell as music and ambient.
But for long things like music it’s not the best solution I think.
How do you do it ? =)

I’ve got implemented JLayer 1.0 to play mp3 background-sound files.

hm I looked at the documentation (API)
how do you simply load and loop backgroundmusic and stuff like that ? how does it work in Jlayer ?

I’d recommend buying a book about Java game programming from Amazon.com. I think “Developing Games in Java” contains an especially clear description, but there are other options available.

You can probably get the book cheap from now because it’s several years old.

Well but if anyone could post these few lines of code to load and play through JLayer, or explain, that would be awesome.

like in OggClip: you can load using the constructor , putting in a string… like this : OggClip sound = new OggClip(“soundfile.ogg”);
and play simply by using .play()…

What in JLayer is the storage, the buffer, how to load and how to play (loop)…

So here’s the sound interface I build for my background FXs :

/** instances a Sound interface
     * @return one sound interfaced with
     * @see Sound*/
    static Sound initSound() {
        return new Sound() {
            AudioClip bgSfx = null;
            Player player = null;
            boolean bgSfx_playing = false;
            boolean mp3Enabled = false;
            String fileRsrc = null;
            public boolean load(String filename) {
                if(filename == null || filename == "")
                    return false;
                URL rsrc = null;
                try {
                    rsrc = (sfxIsRsrc) ? getClass().getResource(filename) : new File(filename).toURL();
                    bgSfx = Applet.newAudioClip(rsrc);
                    player = new Player((sfxIsRsrc)?getClass().getResourceAsStream(filename):new BufferedInputStream(new FileInputStream(filename)));
                } catch (JavaLayerException ex) {
                    ex.printStackTrace();
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                } finally {
                    if(rsrc == null)
                        return false;
                    fileRsrc = filename;
                    return true;
                }
            }
            public void loop() {
                if(mp3Enabled) {
                    bgSfx_playing = true;
                    Thread t_mp3 = new Thread(new Runnable() { public void run() {
                        while(bgSfx_playing) {
                            try {
                                if(!load(fileRsrc)) {
                                    stop();
                                    continue;
                                }
                                if(player instanceof Player)
                                    player.play();
                            } catch (JavaLayerException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }},"T-loop-mp3");
                    t_mp3.start();
                } else {
                    if(bgSfx instanceof AudioClip) {
                        bgSfx_playing = true;
                        bgSfx.loop();
                    }
                }
            }
            public void play() {
                assert fileRsrc != null : "Sound : you must call load(String) once before to play the Sound";
                if(mp3Enabled) {
                    if(!load(fileRsrc))
                        return;
                    Thread t_mp3 = new Thread(new Runnable() { public void run() {
                        try {
                            bgSfx_playing = true;
                            if(player instanceof Player)
                                player.play();
                            bgSfx_playing = false;
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                    }},"T-play-mp3");
                    t_mp3.start();
                } else {
                    if(bgSfx instanceof AudioClip) {
                        bgSfx.play();
                        bgSfx_playing = true;
                    }
                }
            }
            public void stop() {
                if(mp3Enabled) {
                    if(player instanceof Player)
                        player.close();
                    bgSfx_playing = false;
                } else {
                    if(bgSfx instanceof AudioClip)
                        bgSfx.stop();
                    bgSfx_playing = false;
                }
            }
        };
    }

there’s the sfxIsRsrc variable to set statically somewhere around and that be the point. ;D it can play either with the std AudioClip interface or with the JLayer API. As you can see, AudioClip provides loop() as a default method but JLayer doesn’t. Hence I make a new Thread that looks up to a Boolean as each (Player).play()-call finishes. all of the methods can be called at any time asynchronously.
CU! 8)

ya I just figured out a bit how it works:


import javazoom.jl.player.Player;
import java.io.*;

public class jo
{
	
	public static void main(String blah[])
	{
		InputStream s1 = null;
		Player myplayer = null;
		boolean succ = false;
		
		try
		{
			s1 = new FileInputStream( "fight.mp3" );
			myplayer = new Player(s1);
		}
		catch(Exception l){System.out.println("FuckError : "+l.getMessage());}
		
		if (myplayer != null && s1 != null) 
		{
			System.out.println("Playing fight.mp3 \nCrtl + C to abort");
			try{
				myplayer.play();
				succ = true;
				}
			catch(Exception j){System.out.println("Couldn't play because: "+j.getMessage());}
		}
		
		while(succ)
		{	
		}	
	}
}

But whats with :

player = new Player((sfxIsRsrc)?getClass().getResourceAsStream(filename):new BufferedInputStream(new FileInputStream(filename)));

I did it easier, whats the point ? :slight_smile:

And well I tried out AudioClip a while ago, when I started Java… don’t like them :smiley:
And I can also write a loop function somehow…

I just made the special resource switch to have my code a bit more scalable. It can depends whether I run the app with a full .jar , or with resources nearby the .jar… Then I can change the one rsrc variable on compile-time to quickly adapt. Nothing very important but useful… :wink:

but something very disturbing is: the JLayer Player stopsverything when it’s played, unlike AudioClip or OggClip or even Normal Clips…
so I have to do it in a thread… man I avoided threads wherever I could, but I definitely have to learn and use them properly on this now =/