这是一个音频播放的简单程序,是对网上的一个程序改造的,是把它简单化了。
其中的stop()、pause()函数都没有使用。
该程序支持.wav格式的音频,改掉主函数中的音频名字,就可以直接运行。音频播放完会自动关闭。
public class AudioPlay
implements Runnable {
private JavaSoundAudioClip locate, warning;
protected boolean threadExit;
protected boolean stopped;
protected boolean playing;
Thread playerThread;
public AudioPlay() {
}
public void start(File f) {
playing = true;
stopped = false;
try {
FileInputStream ff = new FileInputStream(f);
locate = new JavaSoundAudioClip(ff);
}
catch (Exception e) {
e.printStackTrace();
}
playerThread = new Thread(this);
playerThread.start();
}
public void run() {
if (playing) {
if (!stopped) {
if (locate == null)
System.out.print("nulll");
try {
locate.play();
System.out.print("test");
}
catch (Exception ee) {
ee.printStackTrace();
}
}
}
}
public void stop() {
stopped = true;
threadExit = true;
if (playing == true) {
playing = false;
locate.stop();
}
}
public void pause() {
if (playing == true) {
locate.stop();
}
}
public static void main(String a[]){
AudioPlay sound=new AudioPlay();
try{
File f=new File("Sound/Contagious.wav");//用适当的路径和文件名替换
sound.start(f);
}catch(Exception e){
e.printStackTrace();
}
}
}