Android 分贝测试仪功能

也是那个工具栏上的,测试分贝也就是打开麦克风然后接收音量,用计算公式求出音量的分贝值,通过传递message来修改UI。
有几个关于麦克风的参数
mARecorder = new MediaRecorder(); //声音录制
mARecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //录制的音源为麦克风
mARecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); //设置音频文件的编码
mARecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置audio格式
xml大致如下(重复的省略了)



    
        

Android 分贝测试仪功能_第1张图片
分贝值旁边的两个小喇叭是有动画效果的animation-list

接下来是 MicDemoActivity 完成开启麦克风接收音量功能

package com.rikka.toolbox;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

public class MicDemoActivity extends Activity {
    private MicroPhoneThread microPhone = new MicroPhoneThread();  //线程用于实时录制周围声音
    public boolean istrue = true;

    private MediaRecorder mARecorder;    //麦克风控制
    private File mAudiofile,mSampleDir;  //录音文件保存
    private ImageView iv_record_wave_left,iv_record_wave_right;
    private AnimationDrawable ad_left,ad_right;
    private TextView textView1;
    private MHandler mHandler = new MHandler();

    class MHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    textView1.setText(msg.obj.toString());
            }
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_mic);
        init();
    }

    private void init() {
        //初始化左侧动态动画控件
        iv_record_wave_left = findViewById(R.id.iv_record_wave_left);
        iv_record_wave_right = findViewById(R.id.iv_record_wave_right);
        ad_left = (AnimationDrawable) iv_record_wave_left.getBackground();
        ad_right = (AnimationDrawable) iv_record_wave_right.getBackground();
        ad_left.start();
        ad_right.start();
        textView1 = findViewById(R.id.textView1);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //录音获取麦克风声音
        mARecorder = new MediaRecorder();                                //声音录制
        mARecorder.setAudioSource(MediaRecorder.AudioSource.MIC);       //录制的音源为麦克风
        mARecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); //设置音频文件的编码
        mARecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置audio格式

        try{
            mSampleDir = Environment.getExternalStorageDirectory();   //获取手机内存路径
            //用IM+系统当前时间为文件名建立.amr的文件,文件路径为mSampleDir
            mAudiofile = File.createTempFile("IM" + System.currentTimeMillis(),".amr",mSampleDir);
        } catch (IOException e) {
           Log.e("IMMESSAGE","sdcard access error");
        }

        mARecorder.setOutputFile(mAudiofile.getAbsolutePath()); //设置路径
        try{
            mARecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mARecorder.start();
        microPhone.start();
    }

    //返回功能
    public void onBack(View v){
        MicDemoActivity.this.finish();
    }

    class MicroPhoneThread extends Thread{       //测试当前分贝值通知UI修改
        final float minAngle = (float) Math.PI * 4 / 11;
        float angle;

        @Override
        public void run() {
            while(istrue){
                angle = 100 * minAngle * mARecorder.getMaxAmplitude() / 32768;
                if(angle > 100){
                    angle = 100;
                }
                //构造方法的字符格式这里如果小数不足2位,会已0补足
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                String p = decimalFormat.format(angle);

                mHandler.sendMessage(mHandler.obtainMessage(1,p));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

你可能感兴趣的:(Demo)