Android decibel tester function

It's also on the toolbar. To test the decibel, turn on the microphone and receive the volume. Use the calculation formula to find the decibel value of the volume, and modify the UI by passing the message.
There are several parameters about the microphone
Mrecorder = new mediarecorder(); / / sound recording
Mrecorder. Setaudiosource (mediarecorder. Audiosource. MIC); / / the recorded sound source is microphone
Mrecorder. Setoutputformat (mediarecorder. Outputformat. Raw? AMR); / / set the encoding of audio files
Mrecorder. Setaudioencoder (mediarecorder. Audioencoder. AMR? NB); / / set audio format
xml is roughly as follows (repeated omitted)

<?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="Decibel test"
            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="Breath sounds"
                    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>


The two trumpets next to the decibel value are animation list

Next, MicDemoActivity completes the function of turning on microphone receiving volume

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();  //Threads are used to record ambient sound in real time
    public boolean istrue = true;

    private MediaRecorder mARecorder;    //Microphone control
    private File mAudiofile,mSampleDir;  //Recording file saving
    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() {
        //Initialize left dynamic animation control
        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();
        //Recording for microphone sound
        mARecorder = new MediaRecorder();                                //sound recording
        mARecorder.setAudioSource(MediaRecorder.AudioSource.MIC);       //Recorded sound source is microphone
        mARecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); //Set the encoding of audio files
        mARecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //Format audio

        try{
            mSampleDir = Environment.getExternalStorageDirectory();   //Get mobile memory path
            //Create a file named. amr with the current time of IM + system. The file path is mSampleDir
            mAudiofile = File.createTempFile("IM" + System.currentTimeMillis(),".amr",mSampleDir);
        } catch (IOException e) {
           Log.e("IMMESSAGE","sdcard access error");
        }

        mARecorder.setOutputFile(mAudiofile.getAbsolutePath()); //set up path
        try{
            mARecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mARecorder.start();
        microPhone.start();
    }

    //Return function
    public void onBack(View v){
        MicDemoActivity.this.finish();
    }

    class MicroPhoneThread extends Thread{       //Test the current decibel value to inform UI of modification
        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;
                }
                //The character format of the construction method here if the decimal number is less than 2 digits, it will be 0-filled
                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();
                }
            }
        }
    }

}

Tags: Android encoding Java xml

Posted on Mon, 30 Mar 2020 09:38:53 -0700 by melrse