Music in Java (start/stop)

Austin picture Austin · Mar 12, 2012 · Viewed 18.4k times · Source

Ok, so I am making a game and the music changes when you are in different regions or if there is an interruption, like with an AI.

So I have JUST learned how to make music showup in my program, and now I am trying to make it stop, but I am unsure how to, below is a snippet of code where the music plays and then I try to overwite it with new music when an action occurs.

public static void songs(String word) {
        String temp = word;
        if (temp.equals("start")) {

            try {

                try {
                    blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");

                } catch (Throwable e) {
                    e.printStackTrace();
                }
                AudioStream as = new AudioStream(blah);

                AudioPlayer.player.start(as);
                System.out.println("going");

            } catch (IOException e) {
                System.err.println(e);
            }
        }

        if (temp.equals("stop")) {

            try {

                try {
                    blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/silence.wav");

                } catch (Throwable e) {
                    e.printStackTrace();
                }
                AudioStream as = new AudioStream(blah);

                AudioPlayer.player.stop(as);
                System.out.println("stopping");

            } catch (IOException e) {
                System.err.println(e);
            }
        }
    }

This is the only method I have been able to find that has the music play, but if you guys have any other suggestions please let me know.

Again, I want to have sound affects and music going, and right now all that happens is one song will play, and it will not stop under any circumstance until it hits the very end of its length. I want to be able to stop songs whenever a new one should come on, and also allow sound affects to pop up.

Thanks! (since I am stuck on this and need an answer now I will probably repost on one or two more java sites so I can get a response ASAP, thank you though!!!!)

EDITED CODE: (still does not stop the current stream, any more suggestions appreciated)

public static void songs(String word) throws IOException {
    String temp = word;


    if (temp.equals("go")) {
        try {
            blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
        } catch (Throwable e) {
            e.printStackTrace();
        }

        AudioStream as = new AudioStream(blah);
        AudioPlayer.player.start(as);
        System.out.println("going");
    }

    if (temp.equals("stop")) {

        //don't try and do things with a null object!
        if (as != null) {
            AudioPlayer.player.stop(as);
            System.out.println("stopping1");
        }
        System.out.println("stopping2");
        AudioPlayer.player.stop(as);
    }
}

Answer

raveturned picture raveturned · Mar 12, 2012

Currently you're creating a new AudioStream in your stop branch and calling the stop method using this. This is a different object to the one that is currently playing. Try making the AudioStream a class variable, and calling stop on that instead.

EDIT: at the top of the class containing your code...

class YourClass {
    //the class member variable
    private AudioStream as;
    //[etc...]

In your start branch:

// 'as' has already been defined above
as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");

In your stop branch:

try
{
    //don't try and do things with a null object!
    if (as != null)
    {
        AudioPlayer.player.stop(as);
    }
    System.out.println("stopping");
}
catch (IOException e)
{
    System.err.println(e);
}

You may have trouble with the static identifier on your method - if you're calling this from within an instantiated class you don't need this.