📄 voicesender.java
字号:
/*
* VoiceSender.java
*
* Created on 2006年11月22日, 下午12:30
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cn.edu.tsinghua.thss.talkie.audio;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*子类VoiceSender 就作为第二线程启动的时候,
* startPhone() 方法传递给它的参数是实例化的CallLink 子类curCallLink ,
* 子类VoiceSender 通过实例化的AudioCapture 对象PhoneMIC 将音频信号压缩成GSM数据,
* 并通过curCallLink 将音频数据发送到具有目标IP 地址的计算机上,
* 完成类似电话受话器的功能。
* @author promenade
*/
public class VoiceSender implements Runnable {
InputStream myIStream = null;
OutputStream sendStream = null;
private byte[] compressedVoice = new byte[JPhone.bufSize];
AudioCapture PhoneMIC = null;
CallLink curCallLink = null;
Thread thread2;
/** Creates a new instance of VoiceSender */
public VoiceSender( CallLink curCallLink, AudioCapture PhoneMIC ) {
this.curCallLink = curCallLink;
this.PhoneMIC = PhoneMIC;//new AudioCapture(AMAudioFormat.FORMAT_CODE_GSM);
myIStream = PhoneMIC.getAudioInputStream();
//(new Thread(this)).start();//not easy for debug
thread2 = new Thread( this, " Sending Thread " );
thread2.start();
}
public void stop(){
thread2.interrupt();
}
public synchronized void run(){
boolean continueFlag = false;
//直到能够连接上服务器才继续下面操作
while( !Thread.currentThread().interrupted() && continueFlag==false ){
try {
curCallLink.open();
continueFlag = true;
System.out.println("Connected. Now ready for sending..");
} catch (IOException ex) {
System.out.println("failed to connect");
continueFlag = false;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
try {
sendStream = curCallLink.getOutputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
PhoneMIC.start();
} catch (Exception ex) {
ex.printStackTrace();
}
int b;
while( !Thread.currentThread().interrupted() ){
try {
System.out.println("sending....");
b = myIStream.read(compressedVoice, 0, JPhone.bufSize);
sendStream.write(compressedVoice,0,b);
System.out.println("sending......done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -