當我點擊該框時,音樂工作得很好,但是當我再次點擊它時,音樂不會停止。然後,如果我再次點擊未經檢查的框,音樂再次播放,所以2次一次!請幫助我停止音樂!CheckBox聲音停止
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
InputStream inputStream = getClass().getResourceAsStream("panda - desiigner (donald trump remix).au");
AudioStream audioStream = null;
try {
audioStream = new AudioStream(inputStream);
} catch (IOException ex) {
Logger.getLogger(kekFrame.class.getName()).log(Level.SEVERE, null, ex);
}
int check;
if(jCheckBox1.isSelected() == true){
check = 1;
} else {
check = 0;
}
switch (check) {
case 1 : AudioPlayer.player.start(audioStream);
System.out.println("Music has started playing");
break;
case 0 : AudioPlayer.player.stop(audioStream);
System.out.println("Music has stopped playing");
break;
}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JCheckBox jCheckBox1;
請同時分享AudioPlayer的代碼 –
在哪裏,我該怎麼做? –
@DarkKnight'import sun.audio.AudioPlayer;'是(Oracle)JRE內部沒有記錄的(&'no source available')類。 OP:不要使用這些類。改爲使用'javax.sound.sampled'包中的['Clip'](http://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/Clip.html)。 –