最近博主想作一個app,中間有一個是錄音的功能。因而博主想把UI作的好看一些,想仿照微信或者QQ語音輸入時,可以隨着聲音的大小顯示聲波振幅。因而查找了一些資料,如今把這個功能的Demo分享給你們,之後也會把更多的項目學習到的知識分享給你們。java
其實這個功能主要是依靠MediaRecorder的getMaxAmplitude()方法來得到聲音的振幅,而後依據計算公式分貝的計算公式K=20lg(Vo/Vi) Vo當前的振幅值,Vi基準值爲600來得到分貝數而後在根據分貝來顯示ImageView上的不一樣圖片。這樣就實現了聲波振幅顯示了。android
下面列出主要的函數,後面會給出錄音顯示聲波振幅的Demo下載連接:微信
public RecordDialog(Context context){ this.context=context; dialog_view=LayoutInflater.from(context).inflate(R.layout.dialog_sound, null); //初始化振幅圖片 progressImg[0]=context.getResources().getDrawable(R.drawable.mic_1); progressImg[1]=context.getResources().getDrawable(R.drawable.mic_2); progressImg[2]=context.getResources().getDrawable(R.drawable.mic_3); progressImg[3]=context.getResources().getDrawable(R.drawable.mic_4); progressImg[4]=context.getResources().getDrawable(R.drawable.mic_5); progressImg[5]=context.getResources().getDrawable(R.drawable.mic_6); progressImg[6]=context.getResources().getDrawable(R.drawable.mic_7); dialog=new AlertDialog.Builder(context).setView(dialog_view).show(); // dialog.cancel(); progress=(ImageView) dialog_view.findViewById(R.id.sound_progress); btn_cancel=(ImageView) dialog_view.findViewById(R.id.cancel); btn_submit=(TextView) dialog_view.findViewById(R.id.submit); mic_icon=(ImageView) dialog.findViewById(R.id.mic); dialog_title=(TextView) dialog.findViewById(R.id.title); txt_msg=(TextView) dialog.findViewById(R.id.msg); btn_cancel.setOnClickListener(onCancel); btn_submit.setOnClickListener(onSubmit); }
而後咱們實現一個自定義的接口SoundAmplitudeListen用來處理獲取分貝值以後顯示不一樣的波動圖片:app
private SoundAmplitudeListen onSoundAmplitudeListen=new SoundAmplitudeListen() { @SuppressWarnings("deprecation") @Override public void amplitude(int amplitude, int db, int value) { // TODO Auto-generated method stub if(value>=6){ value=6; } progress.setBackgroundDrawable(progressImg[value]); } };
最後就是錄音時處理分貝的RecodeManager類了:ide
package com.example.voiceviewdemo; import java.io.File; import java.io.IOException; import java.util.Calendar; import java.util.Locale; import android.R.integer; import android.media.MediaRecorder; import android.os.Environment; import android.os.Handler; import android.text.format.DateFormat; public class RecodeManager { private File file;//錄音文件 private MediaRecorder mediaRecorder;//android 媒體錄音類 private SoundAmplitudeListen soundAmplitudeListen;//聲波振幅監聽器 private final Handler mHandler=new Handler(); private Runnable mUpdateMicStatusTimer=new Runnable() { /** * 分貝的計算公式K=20lg(Vo/Vi) Vo當前的振幅值,Vi基準值爲600 */ private int BASE=500; private int RATIO=5; private int postDelayed=200; @Override public void run() { int ratio=mediaRecorder.getMaxAmplitude()/BASE; int db=(int)(20*Math.log10(Math.abs(ratio))); int value=db/RATIO; if(value<0) value=0; if(soundAmplitudeListen!=null){ soundAmplitudeListen.amplitude(ratio, db, value); mHandler.postDelayed(mUpdateMicStatusTimer,postDelayed); } } }; public void startRecordCreateFile() throws IOException{ if(!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)){ return ; } file=new File(Environment.getExternalStorageDirectory()+File.separator+"1"+File.separator+ new DateFormat().format("yyyyMMdd_HHmmss", Calendar.getInstance(Locale.CHINA))+".amr"); mediaRecorder=new MediaRecorder();//建立錄音對象 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);//從麥克風源進行錄音 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//設置輸出格式 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//設置編碼格式 mediaRecorder.setOutputFile(file.getAbsolutePath()); //建立文件 if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } file.createNewFile(); mediaRecorder.prepare(); mediaRecorder.start(); mHandler.post(mUpdateMicStatusTimer); } public File stopRecord(){ if(mediaRecorder!=null){ mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder=null; mHandler.removeCallbacks(mUpdateMicStatusTimer); } return file; } public void setSoundAmplitudeListen(SoundAmplitudeListen soundAmplitudeListen){ this.soundAmplitudeListen=soundAmplitudeListen; } public interface SoundAmplitudeListen{ public void amplitude(int amplitude,int db,int value); } }
效果以下:函數
Demo 資源: http://download.csdn.net/detail/u014132820/9369346post
下面給出博主本身開發的一些小App,分享給你們:學習