1
我是新来的java和尝试添加声音到我的应用程序。我遇到的问题是音乐开始播放,但当音乐方法第二次被调用时,音乐不会停止。我究竟做错了什么?学习java。音频控制问题
import sun.audio.*;
import java.io.*;
public class sound {
private static boolean playing = false;
@SuppressWarnings("restriction")
public static void music() {
try {
// open the sound file as a Java input stream
String soundFile = "backgroundmusic.wav";
InputStream in = new FileInputStream(soundFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
if(playing == false){
AudioPlayer.player.start(audioStream);
playing = true;
}else{
AudioPlayer.player.stop(audioStream);
playing = false;
}
} catch(IOException error) {}
System.out.println(playing);
}
}
现在啊,现在你说它很明显。谢谢您的帮助。 – Kozmk12