首先非常感谢韩顺平老师的这个项目,虽然断断续续的完成了,但是收获很大,很有帮助。
我的这些资源也是从别人那个弄来的,但是这个是整合版的。
//音乐播放代码
import javax.sound.sampled.*;
import java.io.*;
public class PlayAudio extends Thread {
private String filename;
public PlayAudio(String wavfile) {//构造器指定一个文件
String path = System.getProperty("user.dir");
filename = path + "\\" + wavfile;
}
@Override
public void run() {
// 从项目资源目录下加载背景音乐
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(filename)));
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
//缓存
byte[] abData = new byte[512];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
标签:java,音乐文件,nBytesRead,return,auline,new,audioInputStream,abData,顺平
From: https://blog.csdn.net/PAI13/article/details/137371065