packagecom.bshark.letmessage.server;importandroid.media.AudioFormat;importandroid.media.AudioRecord;importandroid.media.MediaRecorder;importandroid.os.Bundle;importandroid.os.Message;importcom.bshark.letmessage.ui.BlowActivity.MyHandler;importcom.bshark.letmessage.util.Parameter;public class RecordThread extendsThread {privateAudioRecord ar;private int bs = 100;private static int SAMPLE_RATE_IN_HZ = 8000;privateMessage msg;private int number = 1;private int tal = 1;privateMyHandler handler;private longcurrenttime;private longendtime;private long time = 1;//到达该值之后 触发事件
private static int BLOW_ACTIVI=2620;publicRecordThread(MyHandler myHandler) {super();
bs=AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCodinG_PCM_16BIT);
ar= newAudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bs);
handler=myHandler;
}
@Overridepublic voidrun() {try{
ar.startRecording();
Parameter.isblow= true;//用于读取的 buffer
byte[] buffer = new byte[bs];while(Parameter.isblow) {
number++;
sleep(8);
currenttime=System.currentTimeMillis();int r = ar.read(buffer, 0, bs) + 1;int v = 0;for (int i = 0; i < buffer.length; i++) {
v+= buffer *buffer;
}int value = Integer.valueOf(v / (int) r);
tal= tal +value;
endtime=System.currentTimeMillis();
time= time + (endtime -currenttime);if (time >= 500 || number > 5) {int total = tal /number;if (total >BLOW_ACTIVI) {//发送消息通知到界面 触发动画//利用传入的handler 给界面发送通知// number = 1;
tal= 1;
time= 1;
}
}
}
ar.stop();
ar.release();
bs=100;
}catch(Exception e) {
e.printStackTrace();
}
}
}