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大致如下(重复的省略了)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">
    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#0000">
        <Button
            android:id="@+id/bt_back"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_margin="5dp"
            android:background="@drawable/back_bg"
            android:onClick="onBack"/>
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:gravity="center_vertical"
            android:text="分贝测试"
            android:textColor="@android:color/white"
            android:layout_centerHorizontal="true"
            android:textSize="20dp"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_record_wave_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/record_wave_left" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_red_light"
            android:textSize="40sp"
            android:text="0.00"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dB"
            android:textColor="@android:color/holo_red_light"
            android:textSize="40sp"
            android:textStyle="bold"/>
        <ImageView
            android:id="@+id/iv_record_wave_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/record_wave_right" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/absclv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/rl"
        android:focusable="false"
        android:focusableInTouchMode="true"
        android:background="@android:color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="10dB"
                    android:textColor="@android:color/holo_green_light"
                    android:textSize="20dp"
                    android:textStyle="bold" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="呼吸声"
                    android:textColor="@android:color/darker_gray"
                    android:textSize="20sp"/>
            </LinearLayout>
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>
                。。。。
    </ScrollView>
</LinearLayout>

这里写图片描述
分贝值旁边的两个小喇叭是有动画效果的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();
                }
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/rikkatheworld/article/details/80076211