首发地址:
http://www.eoeandroid.com/thread-206393-1-1.html
不错的帖子:
Android 快速滑动 list 仿QQ通讯录
http://www.eoeandroid.com/thread-211137-1-1.html
-------------帖子正文-------------------
最近研究腾讯吹一吹,写了一个dome 贴出核心的线程代码,那么吹一吹的原理是什么呢? 声音 对 就是声音.
1.当用户到吹一吹界面之后 有一个核心的线程便开启一直在读取麦克风的声音信息.
2.当500毫秒以内或者读取的5次数据的平均值超过某一个值的时候 则就可以触发吹一吹事件
3.通知界面,界面接受到之后 响应响应的动画。
以下是线程的代码:
package com.bshark.letmessage.server; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Message; import com.bshark.letmessage.ui.BlowActivity.MyHandler; import com.bshark.letmessage.util.Parameter; public class RecordThread extends Thread { private AudioRecord ar; private int bs = 100; private static int SAMPLE_RATE_IN_HZ = 8000; private Message msg; private int number = 1; private int tal = 1; private MyHandler handler; private long currenttime; private long endtime; private long time = 1; //到达该值之后 触发事件 private static int BLOW_ACTIVI=2620; public RecordThread(MyHandler myHandler) { super(); bs = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); ar = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bs); handler = myHandler; } @Override public void run() { 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(); } } }