java sound

来源:互联网 发布:telnet 端口打开失败 编辑:程序博客网 时间:2024/05/21 19:36

javax.sound.sampled包


第一种

public class VoiceThread implements Runnable{


private String path = "alarm.au";

public VoiceThread(){}


public static void main(String[] args) {

VoiceThread.play();
}

@Override
public void run() {

playClip(path);
}


public static void play(){

//最好不要自己启动线程,利用UI线程(即display)异步执行自定义线程声音播放才是swt多线程要点
Display.getDefault().asyncExec(new VoiceThread()); 
// new Thread(player).start();  
}


//播放
private void playClip(String audioFile)
{
   try
   {
    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(getFilePath(audioFile)));
       DataLine.Info info =new DataLine.Info( Clip.class, inputStream.getFormat() );
       final Clip clip = (Clip) AudioSystem.getLine(info);
       clip.addLineListener(new LineListener() {
               public void update(LineEvent e) {
                   if (e.getType() == LineEvent.Type.STOP) {
                       synchronized(clip) {
                       clip.notify();
                       }
                   }
               }
           });
       clip.open(inputStream);       
       clip.setFramePosition(0);
       clip.start();
       synchronized (clip) {
           clip.wait();
       }
       clip.drain();
       clip.close();
       inputStream.close();
   }
   catch (Exception ex)
   {
       ex.printStackTrace();
   }
}

//获取资源文件路径  path:文件名 (注:该文件必须在根目录下)
private String getFilePath(String fileName) throws IOException{

URL url = FileLocator.find(Platform.getProduct().getDefiningBundle(), new Path("/"+fileName), null);
return FileLocator.toFileURL(url).getPath();
}



二、

public class Player {


private AudioFormat format;
private byte[] samples;
private String path = "d:/alarm.au";

public static void main(String args[]) throws Exception {

Player sound = new Player("d:/alarm.au");
// sound.play(stream);
sound.play();
}


public Player(){}

public Player(String filename) throws Exception {

AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));
format = stream.getFormat(); //获取格式
samples = getSamples(stream); //解码为字节数组
}


public void play(){

AudioInputStream audioStream = null;
try {
String filePath = getFilePath(path);
audioStream = AudioSystem.getAudioInputStream(new File(filePath));
format = audioStream.getFormat();
samples = getSamples(audioStream);
InputStream byteStream = new ByteArrayInputStream(samples);
play(byteStream);
audioStream.close(); //关闭流
byteStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}


//解码为字节数组
private byte[] getSamples(AudioInputStream audioStream) {
int length = (int) (audioStream.getFrameLength() * format.getFrameSize());
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
is.close();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return samples;
}


//流播放
private void play(InputStream source) {
int bufferSize = format.getFrameSize()* Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
SourceDataLine line = null;
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufferSize);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
line.start();
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead = source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
line.drain();
line.close();
}

//获取资源文件路径  path:文件名 (注:该文件必须在根目录下)
private String getFilePath(String fileName) throws IOException{

URL url = FileLocator.find(Platform.getProduct().getDefiningBundle(), new Path("/"+fileName), null);
return FileLocator.toFileURL(url).getPath();

}








0 0
原创粉丝点击