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" />

<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”/>

<View

android:layout_width=“wrap_content”

android:layout_height=“1dp”

android:background="@android:color/darker_gray"/>

。。。。

这里写图片描述

分贝值旁边的两个小喇叭是有动画效果的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

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取

;

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();

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐