I have inserted a background audio into my program using AudioInputStream
.
When I press the On
, the background audio will play, and Off
will stop playing the background audio.
Here is the part of my code for the audio playing part :
public void playSound(int i)
{
try
{
. //Others buttons sound
. //Others buttons sound
if(i == 3 || i == 4)
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
Clip clip3 = AudioSystem.getClip( );
clip3.open(audioInputStream);
if(i == 3)
{
clip3.loop(Clip.LOOP_CONTINUOUSLY);
clip3.start();
settingSubMenuItem3.setEnabled(false);
settingSubMenuItem4.setEnabled(true);
}
if(i == 4)
{
clip3.stop();
settingSubMenuItem3.setEnabled(true);
settingSubMenuItem4.setEnabled(false);
}
}
catch(Exception e)
{
System.out.println("Error with playing sound.");
e.printStackTrace();
}
}
And here is the ButtonListener
part :
private class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == settingSubMenuItem3)
{
playSound(3);
}
if(e.getSource() == settingSubMenuItem4)
{
playSound(4);
}
}
}
My code can play the audio when I press On
button, but when I press Off
button, it does not work.
There is no error during compile and run.
What is the error of my code?