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!!!!)

you're trying to 'stop' a different AudioStream then the one you've 'start'ed.

AudioStream as = new AudioStream(blah);
AudioPlayer.player.start(as);

this is the audiostream you are playing, so it would be this particular stream you would have to stop.

AudioStream as = new AudioStream(blah);
AudioPlayer.player.stop(as);

this stream, even though it's of the same type and has the same name as the one above, is in no way related to the above one.

you should create that AudioStream on class scope, and use the same one for both the start and stop.

I have been trying that but it's still not working. Is there any way I can stop the stream? Like whether it be audio stream or file input stream?

UPDATED CODE: Still not working, the stream does not stop when told too.

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

This shows up too..

C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:19: warning: sun.audio.AudioPlayer is Sun proprietary API and may be removed in a future release
import sun.audio.AudioPlayer;
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:20: warning: sun.audio.AudioStream is Sun proprietary API and may be removed in a future release
import sun.audio.AudioStream;
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:30: warning: sun.audio.AudioStream is Sun proprietary API and may be removed in a future release
private static AudioStream as;
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:233: warning: sun.audio.AudioStream is Sun proprietary API and may be removed in a future release
AudioStream as = new AudioStream(blah);
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:233: warning: sun.audio.AudioStream is Sun proprietary API and may be removed in a future release
AudioStream as = new AudioStream(blah);
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:234: warning: sun.audio.AudioPlayer is Sun proprietary API and may be removed in a future release
AudioPlayer.player.start(as);
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:242: warning: sun.audio.AudioPlayer is Sun proprietary API and may be removed in a future release
AudioPlayer.player.stop(as);
C:\Users\Austin\Documents\NetBeansProjects\FunGame\src\fungame\PokemonAttack0.java:246: warning: sun.audio.AudioPlayer is Sun proprietary API and may be removed in a future release
AudioPlayer.player.stop(as);
8 warnings

Got it, thanks for any/all help

and? what was the problem?

guess he got it from here since he tried both forums and they were quicker!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.