0
我想在调用方法getoutput
时播放音乐,并在调用完成时停止播放音乐。我能够做到前者而不是后者。完成调用getoutput
方法后,如何停止音乐?如何停止Java中的音频?
import java.io.InputStream;
import javax.sound.sampled.AudioSystem;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Student {
public void music() {
try {
// open the sound file as a Java input stream
String gongFile = "C:\\Users\\wei liang\\Documents\\Programming fundamentals\\T7-Arrays\\Assignment\\TT.wav";
InputStream in = new FileInputStream(gongFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
} catch (Exception ex) {
System.out.println("Error! Can't find file.");
ex.printStackTrace();
}
}
}
这是我调用音乐方法和其他方法在Student类中未显示的主要方法。
public class StudentUser {
//Main method
public static void main(String args[]) {
//creating a new Student object
Student stud = new Student();
//Calling the music method
stud.music();
//Calling the getoutput method
stud.getoutput();
}
}
不要使用'太阳*'包,他们是无证和随时都可能消失。您应该使用'AudioInputStream'和'Clip'来存储/播放音频。 – Brendan
如果你需要答案让我知道,我写信给你,你没有回应。 –