[code=Java]import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
/** 提供包装和播放音频文件的类,通过对此类的关联进行应用
* @author snakewarhead
* @version 1.0
*/
public class AudioManagement{
private File file;
public AudioManagement(String fileName){
file = new File(fileName);
}
//播放音频文件
public void play(){
try{
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
AudioFormat aif = ais.getFormat();
SourceDataLine sdl = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,aif);
sdl = (SourceDataLine)AudioSystem.getLine(info);
sdl.open(aif);
sdl.start();
//play
int nByte = 0;
byte[] buffer = new byte[128];
while(nByte != -1){
nByte = ais.read(buffer,0,128);
if(nByte >= 0){
int oByte = sdl.write(buffer, 0, nByte);
//System.out.println(oByte);
}
}
sdl.stop();
}catch(UnsupportedAudioFileException e){
e.printStackTrace();
} catch (IOException e) {
// TODO 自动产生 catch 区块
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO 自动产生 catch 区块
e.printStackTrace();
}
}
}[/code]