Reputation: 3
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);
}
}
Upvotes: 0
Views: 610
Reputation: 987
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
Upvotes: 1