【实例简介】
【实例截图】
【核心代码】
package me.daei.soundmeter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import me.daei.soundmeter.widget.SoundDiscView;
public class MainActivity extends AppCompatActivity {
private boolean bListener = true;
private boolean isThreadRun = true;
private Thread thread;
float volume = 10000;
private SoundDiscView soundDiscView;
private MyMediaRecorder mRecorder ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecorder = new MyMediaRecorder();
}
private void startListenAudio() {
thread = new Thread(new Runnable() {
@Override
public void run() {
while (isThreadRun) {
try {
if(bListener) {
volume = mRecorder.getMaxAmplitude(); //获取声压值
if(volume > 0 && volume < 1000000) {
World.setDbCount(20 * (float)(Math.log10(volume))); //将声压值转为分贝值
soundDiscView.refresh(); //刷新View [注]子线程
}
}
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
bListener = false;
}
}
}
});
thread.start();
}
/**
* 开始记录
* @param fFile
*/
public void startRecord(File fFile){
try{
mRecorder.setMyRecAudioFile(fFile);
if (mRecorder.startRecorder()) {
startListenAudio();
}else{
Toast.makeText(this, "启动录音失败", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Toast.makeText(this, "录音机已被占用或录音权限被禁止", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
soundDiscView = (SoundDiscView) findViewById(R.id.soundDiscView);
bListener = true;
File file = FileUtil.createFile("temp.amr");
if (file != null) {
Log.v("file", "file =" file.getAbsolutePath());
startRecord(file);
} else {
Toast.makeText(getApplicationContext(), "创建文件失败", Toast.LENGTH_LONG).show();
}
}
/**
* 停止记录
*/
@Override
protected void onPause() {
super.onPause();
bListener = false;
mRecorder.delete(); //停止记录并删除录音文件
thread = null;
}
@Override
protected void onDestroy() {
if (thread != null) {
isThreadRun = false;
thread = null;
}
mRecorder.delete();
super.onDestroy();
}
}