Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:22:37+00:00 2026-05-31T07:22:37+00:00

Ok, so I am making a game and the music changes when you are

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:22:38+00:00Added an answer on May 31, 2026 at 7:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a game where I have must have some objects like weapons and
I'm making a game and would like to have a background loop of game
I am making a game. I would like to have a joystick that moves
I'm making a game in XNA, but since I have people with different operating
I'm making a game. Now if my player goes to another level the music
I built a slideshow/decision-making game in Flash but would like to try to redo
I'm making this game where I'm trying to pair people. So I have this
I am making a game for my CS class and the sprites I have
I am making a game that needs a crosshair. I have been playing with
Im trying to make a game for the iPhone that has background music. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.