利用录音AudioRecord实现吹一吹效果

不错的创意,记一下, 原帖

原理其实就是话筒的监听,需要在AndroidManifest.xml中要加入两个权限
   
   
另外模拟器无法测试,只能用真机才有效果
另外:低于API 14的SDK可能无效,我测试小米android2.3.5似乎无效,三星9300 android4.0有效!
以下是copy的代码,留在此处以便将来有不时之需。

package com.li;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BlowActivity extends Activity {
	static TextView text;
	Button btn;
	Button btn1;
	static int i=0;
	RecordThread tt=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=(TextView)findViewById(R.id.text);
        btn=(Button)findViewById(R.id.btn);
		btn.setOnClickListener(listener);
    }
    
    class MyHandler extends Handler{
    	
    }
    
    OnClickListener listener=new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
				tt=new RecordThread(r);           //点击按钮,启动线程
				tt.start();
		}
    	
    };
    
    MyHandler r=new MyHandler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);          //接收到message后更新UI,并通过isblow停止线程
			text.setText("你吹了一下屏幕"+i);
			Parameter.isblow=false;
	
		}
    };
}


package com.li;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Message;

import com.li.BlowActivity.MyHandler;

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 = 3000;

	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[i] * buffer[i]);
				}
				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 给界面发送通知
						BlowActivity.i += 1;
						handler.sendEmptyMessage(0); // 改变i的值后,发送一个空message到主线程

						//
						number = 1;
						tal = 1;
						time = 1;
					}
				}

			}
			ar.stop();
			ar.release();
			bs = 100;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


public class Parameter {
	static boolean isblow;
}
  • Blow.rar (184.3 KB)
  • 下载次数: 118

你可能感兴趣的:(利用录音AudioRecord实现吹一吹效果)