80,492
社区成员
发帖
与我相关
我的任务
分享 RecordPlayThread() {
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
playBufSize = AudioTrack.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
Log.e("test", "bufsize: "+recBufSize);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
} public void run() {
socket = new Socket();
try {
socket.connect(localInetSocketAddress, 3000);
// TODO TIMEOUT是否影响发送延迟?
byte[] buffer = new byte[recBufSize];
//byte[] buffer = new byte[32];
DataOutputStream localOutputStream = new DataOutputStream(
socket.getOutputStream());
audioRecord.startRecording();// 开始录制
int bufReadLen = audioRecord.read(buffer, 0, buffer.length);
while (recording) {
audioRecord.read(buffer, 0, buffer.length);
localOutputStream.write(buffer, 0, bufReadLen);
// TODO 或者是发送缓存的影响?
}
audioTrack.stop();
audioRecord.stop();
} catch (Throwable t) {
}
} class LoopDataThread extends Thread {
private Socket s;//接收线程传过来的socket
private InputStream is;//socket里面的input流
private AudioFormat format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, 44100.0f, 16, 1, 2, 44100.0f,
false);
private SourceDataLine line;
private byte[] data;
public LoopDataThread() {
try {
MouseUI.message.append("接收语音 ...\n");
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
format);
line = (SourceDataLine) AudioSystem.getLine(info);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
synchronized (WavPlayer.this.sockets) {
WavPlayer.this.sockets.add(this.s);
}
try {
line.open(format);
line.start();
data = new byte[32];
int i=1;
while (true) {
if (isAct) {
line.open(format);
MouseUI.message.append(i+" open ...\n");
} else {
line.close();
MouseUI.message.append(i+" close...\n");
}
is.read(data);
line.write(data, 0, data.length);
MouseUI.message.append(i+" play ...\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}