標籤:android應用 webview 話筒音頻採樣
Android手機話筒採樣有部分代碼是在網上找的,具體不記得了。
使用採樣結果以及是自己的app的功能代碼:
1. 在一部分app中會需要對話筒的音頻輸入擷取使用,比如之前很火的吹裙子;
2. 採樣線程:
public class MicroLevelRunnable implements Runnable {
private static final String tag = "MicroLevelThread";
private AudioRecord ar;
private short bs;
// 11 025 Hz、8 bit的聲音稱為電話音質;
// 22 050 Hz、16 bit的聲音稱為廣播音質;
// 44 100 Hz、16 bit已達到CD的音質了[4]。
private final int SAMPLE_RATE_IN_HZ = 22050;
private boolean isRun = false;
public static final int msg_v = 0;
public static final int msg_vv = 1;
public static final int msg_vf = 2;
public static final int msg_vo = 3;
public static final int page_need_water = 10;
public static int bubble_speed = 2;// 每N次調用,出一個bubble
public MicroLevelRunnable() {
super();
}
public void stop(){
isRun = false;
ar.stop();
ar.release();
ar = null;
}
public void run() {
bs = (short)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);
// 用於讀取的
ar.startRecording();
short[] buffer = new short[bs];
isRun = true;
int speed = 1;
while (true) {
if (!isRun) {
try {
synchronized (this) {
Log.v(tag, "wait..");
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
speed++;
int r = ar.read(buffer, 0, bs);
int v = 0;
for (int i = 0; i < buffer.length; i++) {
v += buffer[i] * buffer[i];
}
int value = (int) (Math.abs((int) (v / (float) r) / 10000) >> 1);
double dB = 10 * Math.log10(Math.abs(v) / (double) r);
Message msg = new Message();
msg.what = msg_v;
msg.arg1 = value;
msg.arg2 = (int) dB;
// -------------------------------------------
// 用以下方法區分是否是吹氣,缺點是可以類比
// DB白噪低於50為噪音,大於60為無效
// value值低於38為無聲,大於60無無效
if (speed > bubble_speed) {
BubbleMain.MainHandle.sendMessage(msg);
speed = 1;
}
}
}
}
3. 啟動採樣線程:
mRunnable = new MicroLevelRunnable();
mThread = new Thread(mRunnable);
mThread.start();
4. 本人的android應用【http://openbox.mobilem.360.cn/index/d/sid/341284 http://zhushou.360.cn/detail/index/soft_id/341284】 吹泡泡中就使用了如上功能模組
該應用是web+android組件的典型應用,吹泡泡的展現是最佳化修改過的web程式,通過webview介面以及話筒採樣線程的結合,實現在手機上吹出泡泡的效果:
Android話筒音頻採樣和使用