基本信息
源码名称:android 声音强度、分贝 大小检测 源码下载(as版)
源码大小:0.14M
文件格式:.zip
开发语言:Java
更新时间:2016-08-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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(); } }