java中怎样利用javax.sound来播放音乐,最好给一个例子
发布网友
发布时间:2022-05-06 09:57
我来回答
共1个回答
热心网友
时间:2022-06-29 07:53
*该方法为音频流缓冲方法,在调用音频播放线程时调用
*/
public BufferedInputStream loadBomb(){
BufferedInputStream bufbomb = null;
try{
File dir = new File("D:/java/TankWar2.6/"); //所要播放文件的路径
File fObject = new File(dir,"bomb.wav"); //音频名
FileInputStream file = new FileInputStream(fObject);
bufbomb = new BufferedInputStream(file);
}catch(IOException e) {
System.out.println(e.getMessage());
}
return bufbomb;
}
/**
* 缓冲完毕的核爆音频播放线程
*/
private class BombMusic implements Runnable{ //通过流读取声音文件
BufferedInputStream buf;
public void run(){
buf = loadBomb();
try{
AudioStream audio = new AudioStream(buf);
AudioPlayer.player.start(audio);
}catch(IOException e){
e.printStackTrace();
}
}
}