项目里面包含音频文件,怎么包音频文件打包进jar?
代码如下
package cn.tank;
import java.io.*;
import javax.sound.sampled.*;
public class TestAudio {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建一个AePlayWave 对象实例
AePlayWave apw=new AePlayWave("src/sound/music.wav");
//2.启动该线程播放
apw.start();
}
}
//播放声音的类
class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} 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();
}
}
}
Swing
------解决方案--------------------音频文件放进src/main/resources