简体   繁体   中英

Music in Java (start/stop)

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);
    }
}

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.

I can't even access these sun.audio Objects on my Eclipse IDE--I know they are in rt.jar, but there is header info about them being proprietary and such.

Can the Java Sound library (javax.sound.sampled) handle what you want to do? Both Clip and SourceDataLine allow one to stop playback. That is a more usual way of playing sound, if you want to use native Java.

Playback into is here:

http://docs.oracle.com/javase/tutorial/sound/playing.html

But the documentation, overall, is not exactly rich with examples. There's example code at this site

http://www.jsresources.org/

and plenty of people here who could help if you run into problems with the native Java approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM