简体   繁体   中英

audio - Clip won't stop

I have a function that is called every time the user clicks which will start the music if the screen has changed. The only problem with it right now is that it does not stop. I have figured out that this is because clip = AudioSystem.getClip(); won't return the currently playing clip, it returns a random location every time. How do I make sure that clip gets the value of the currently playing clip so that I can stop it?

The "StartBGMusic" function

public void startBGMusic() throws LineUnavailableException, IOException
    {   
        clip = AudioSystem.getClip();
        System.out.println(clip);
        try
        {
            if(SCREEN_CHECK != Screen)
            {
                clip.stop();

                clip.close();

                if(Screen != 14 && Screen != 19 && Screen != 16 && Screen != 15 && Screen != 17)
                {
                    clip.open(AudioSystem.getAudioInputStream(getClass().getResource("UpbeatFunk.wav")));
                    clip.loop(99999);
                }
                else
                {
                    clip.open(AudioSystem.getAudioInputStream(getClass().getResource("Background Music.mid")));
                    clip.loop(99999);
                }
            } 

            SCREEN_CHECK = Screen;
        }catch (Exception e)
        {
            e.printStackTrace(System.out);
        }
    }

According to oracle documentation, getClip() does not return the current playing sound, but a clip object that can be used to play back an audio file.

You will need to pass a reference to the clip you used to start the audio file. This can be done by returning it/passing it as an argument or by using global variables/getter methods

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