?? udpcapture.java
字號:
/*
* UDPCapture.java
*
* Created on 2006年5月7日, 下午1:06
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package skyqq;
import javax.sound.sampled.*;
import java.net.*;
/**
* 抓取聲音并發送
* @author wjhua
*/
public class UDPCapture implements Runnable{
TargetDataLine line;
Thread thread;
DatagramPacket pacToSend;
DatagramSocket soc;
String ip;
/** Creates a new instance of UDPCapture */
public UDPCapture(String ip) {
this.ip=ip;
}
public void start() {
thread = new Thread(this);
thread.setName("udpcapture");
thread.start();
}
public void stop() {
thread = null;
}
public void run() {
try {
soc = new DatagramSocket();
//建立輸出流 此處可以加套壓縮流用來壓縮數據
} catch (Exception ex) {
return;
}
AudioFormat format = new AudioFormat(8000, 16, 2, true, true);
//audioformat(float samplerate, int samplesizeinbits, int channels,
// boolean signed, boolean bigendian)
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
try {
line = (TargetDataLine) AudioSystem.getLine(info);
//TargetDataLine 接口是DataLine接口的一種,通過它就可以直接從音頻硬件獲取數據了
line.open(format, line.getBufferSize());
} catch (Exception ex) {
return;
}
byte[] data = new byte[1024];//跟下面的1024應保持一致
int numbytesread = 0;
line.start();
while (thread != null) {
numbytesread = line.read(data, 0, 1024);
try {
pacToSend = new DatagramPacket(data,data.length,
InetAddress.getByName(ip),20001);
soc.send(pacToSend);//寫入網絡流
} catch (Exception ex) {
break;
}
}
line.stop();
line.close();
line = null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -