Android advanced-sonic amplitude display, android advanced Amplitude

Source: Internet
Author: User

Android advanced-sonic amplitude display, android advanced Amplitude

Recently, a blogger wants to create an app with a recording function in the middle. As a result, the blogger wants to make the UI look better. When he wants to simulate or QQ voice input, he can display the acoustic amplitude with the sound size. So I searched for some materials and shared the Demo of this function to everyone. I will share my knowledge about more projects in the future.

In fact, this function mainly relies on the getmaxamplder () method of MediaRecorder to obtain the sound amplitude. Then, based on the calculation formula dB, K = 20lg (Vo/Vi) Vo's current amplitude value, the Vi reference value is 600 to get the number of decibels and then display different images on the ImageView according to the sub-bay. In this way, the acoustic amplitude is displayed.

The main functions are listed below. The Demo download link for the recording to display the acoustic amplitude will be provided later:

Public RecordDialog (Context context) {this. context = context; dialog_view = LayoutInflater. from (context ). inflate (R. layout. dialog_sound, null); // initialize the amplitude image 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 );}

  

Then we implement a custom interface SoundAmplitudeListen to display different fluctuating images after obtaining the decibel value:

 

 

private SoundAmplitudeListen onSoundAmplitudeListen=new SoundAmplitudeListen() {@SuppressWarnings("deprecation")@Overridepublic void amplitude(int amplitude, int db, int value) {// TODO Auto-generated method stubif(value>=6){value=6;}progress.setBackgroundDrawable(progressImg[value]);}};

  


The RecodeManager class for processing decibels during recording:

 

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; // recording File private MediaRecorder mediaRecorder; // android media recording class private Sound AmplitudeListen soundAmplitudeListen; // acoustic amplitude listener private final Handler mHandler = new Handler (); private Runnable mupdatemicstatustable = new Runnable () {/*** dB formula K = 20lg (Vo/Vi) Vo current amplitude value, Vi reference value: 600 */private int BASE = 500; private int RATIO = 5; private int postDelayed = 200; @ Overridepublic void run () {int ratio = mediaRecorder. getMaxAmplitude ()/BASE; int db = (int) (20 * Math. log10 (Math. abs (ratio); int value = db/RATI O; if (value <0) value = 0; if (soundAmplitudeListen! = Null) {response. amplatio (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 (); // create a recording object mediaRecorder. setAudioSource (MediaRecorder. audioSource. DEFAULT); // starts the recording from the microphone source mediaRecorder. setOutputFo Rmat (MediaRecorder. outputFormat. MPEG_4); // set the output format mediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. DEFAULT); // sets the encoding format mediaRecorder. setOutputFile (file. getAbsolutePath (); // create a file if (! File. getParentFile (). exists () {file. getParentFile (). mkdirs ();} file. createNewFile (); mediaRecorder. prepare (); mediaRecorder. start (); mHandler. post (mupdatemicstatustder);} 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 amplen (int amplitude, int db, int value );}}

  

 

The effect is as follows:

 

Demo resources: http://download.csdn.net/detail/u014132820/9369346

The following are some small apps developed by the blogger and shared with you:

 

QR code generator: http://shouji.baidu.com/software/item? Docid = 8395189 & from = as old reader: http://shouji.baidu.com/game/item? Docid = 8425923 & from = as: http://shouji.baidu.com/game/item? Docid = 8409245 & from = as2048 rank edition: http://shouji.baidu.com/game/item? Docid = 8372779 & from = as crazy finger: http://shouji.baidu.com/game/item? Docid = 8269676 & from =

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.