?? capture.java
字號:
package org.phyeas.server;
//音頻捕獲部分,
//Capture.java
import java.io.*;
import javax.sound.sampled.*;
import java.net.*;
/**
* Title: VoiceChat Description: 音頻捕捉(錄音程序) Copyright: Copyright (c) 2001
* Company:
*
* @author
* @version 1.0
*/
public class Capture implements Runnable {
TargetDataLine line;
Thread thread;
Socket s;
BufferedOutputStream captrueOutputStream;
public Capture(Socket s) {// 構造器 取得socket以獲得網絡輸出流
this.s = s;
}
public void start() {
thread = new Thread(this);
thread.setName("Capture");
thread.start();
}
public void stop() {
thread = null;
}
public void run() {
try {
captrueOutputStream = new BufferedOutputStream(s.getOutputStream());// 建立輸出流
// 此處可以加套壓縮流用來壓縮數據
} catch (IOException ex) {
return;
}
AudioFormat format = new AudioFormat(8000, 16, 2, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (Exception ex) {
return;
}
byte[] data = new byte[1024];// 此處的1024可以情況進行調整,應跟下面的1024應保持一致
int numBytesRead = 0;
line.start();
while (thread != null) {
// 取數據(1024)的大小直接關系到傳輸的速度, 一般越小越快
numBytesRead = line.read(data, 0, 128);
try {
captrueOutputStream.write(data, 0, numBytesRead);// 寫入網絡流
} catch (Exception ex) {
break;
}
}
line.stop();
line.close();
line = null;
try {
captrueOutputStream.flush();
captrueOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -