See QQ, have intercom function, how tall on ah, we also come to get a look effect.
That's the effect! Then post the code:
Package Cn.com.zte.uc.ui;import Java.io.ioexception;import Java.util.timer;import java.util.timertask;import Android.app.dialog;import Android.content.context;import Android.media.mediarecorder;import Android.os.Handler; Import Android.view.view;import Android.widget.textview;import Cn.com.zte.uc.r;import Cn.com.zte.uc.utils.mediautils;public class Recorderdialog extends Dialog {private static final long max_duration = 60000 ;p rivate static final long SECOND = 1000;private TextView tv;private View v;private Timer time;private mytimertask TASK;PR Ivate mediarecorder recorder;private long duration;private int[] values = {r.drawable.bg_recorder_value_0, R.drawable.bg_recorder_value_1,r.drawable.bg_recorder_value_2, R.drawable.bg_recorder_value_3, R.drawable.bg_ Recorder_value_4,r.drawable.bg_recorder_value_5, R.drawable.bg_recorder_value_6, R.drawable.bg_recorder_value_7, R.drawable.bg_recorder_value_8};p ublic Recorderdialog (context context) {Super (context, r.style.style_dialog); SetcontentvieW (r.layout.dlg_recorder); time = new Timer (), TV = (TextView) Findviewbyid (r.id.tv_duration); v = Findviewbyid (R.id.ll_ REC);} public void Show (String path) {tv.settext ("60\"); setduration (0); recorder = mediautils.getrecorder (path); try { Recorder.prepare (); Recorder.start ();} catch (IllegalStateException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} task = new Mytimertask (); Time.schedule (task, SECOND); Super.show ();} public void Show () {} @Overridepublic void dismiss () {if (!isshowing ()) {return;} Super.dismiss (); Try{task.cancel (); Recorder.stop (); Recorder.release ();} catch (Exception e) {e.printstacktrace ();}} public int getduration () {return (int) (duration/second);} public void setduration (int duration) {this.duration = Duration * SECOND;} Class Mytimertask extends TimerTask {@Overridepublic void run () {int ratio = Recorder.getmaxamplitude ()/600;int db = 0;/ /decibels can also be understood as defined volume size if (ratio > 1) db = (int) (MATH.LOG10 (ratio));//db is the value of the volume we need to get. (int) (* math.log10(ratio)) The amplitude and volume size of the formula//base the value obtained by your own test, how do I get this worth? Turn on the microphone and don't speak to the microphone, while the value from the ambient noise is around 300 to 600 I got the benchmark value of 600. Handler.sendemptymessage (DB/5);}} Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {if (msg.what >= values.length) {msg. what = values.length-1;} V.setbackgroundresource (Values[msg.what]); if (duration = = max_duration) {dismiss ();} else {duration + = 100;} System.out.println (duration); Tv.settext ((max_duration-duration)/second + "\" ");};};
There is also a tool class:
/* Media related tools * Sealed Chen Yu */package cn.com.zte.uc.utils;import java.io.bytearrayoutputstream;import java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.randomaccessfile;import Android.media.mediarecorder;import Android.util.log;import Cn.com.zte.uc.constant.constants;public class Mediautils {/** * Get a Mediarecorder object, output AMR file to path * * @param path * @retur n */public Static Mediarecorder Getrecorder (String path) {Mediarecorder recorder = new Mediarecorder (); File File = new file (Constants.sdcard_path + "RECORD" + file.separator), if (!file.exists ()) {file.mkdirs ();} File = new file (path), if (File.exists ()) {File.delete ();} try {file.createnewfile ();} catch (IOException e) {log.e ("recorder", "Create new Mediarecorder file error!");} Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Source of Recorded Sound Recorder.setoutputformat (MediaRecorder.OutputFormat.RAW_AMR); Recorder.setaudioencoder ( MediaRecorder.AudioEncoder.AMR_NB); recorder.seToutputfile (File.getabsolutepath ()); recorder.setaudiosamplingrate (8000); Recorder.setaudioencodingbitrate (16); return recorder;} /** * Use the recording on the SD card * * @throws FileNotFoundException * */public static byte[] Loadsdcardrecordfile (String path) throws Filen otfoundexception {if (stringutils.isempty (path)) {return null;} byte[] img = NULL; File File = new file (path), if (File.exists ()) {byte[] temp = new byte[4096]; FileInputStream fos = null; Bytearrayoutputstream out = null;try {fos = new FileInputStream (file), out = new Bytearrayoutputstream (); int size = 0;while (size = Fos.read (temp))! =-1) {out.write (temp, 0, size);} img = Out.tobytearray ();} catch (Exception e) {log.e ("recorder", "io error!"),} finally {try {if (fos! = null) {Fos.close ();} if (out! = null) {Out.close ();}} catch (IOException e) {log.e ("recorder", "io error!");}}} return img;} /** * Time to get AMR file * @param file * @return * @throws ioexception */public static long getamrduration (String path) throws IOE xception {File File = new file (path);Long Duration = -1;int[] Packedsize = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; Randomaccessfile Randomaccessfile = null;try {randomaccessfile = new Randomaccessfile (file, "RW"); Long length = File.leng th ();//File length int pos = 6;//set initial position int framecount = 0;//initial frame number int packedpos = -1;byte[] Datas = new byte[1];//initial data value while (p Os <= length) {Randomaccessfile.seek (POS); if (Randomaccessfile.read (datas, 0, 1)! = 1) {duration = length > 0? ((length-6)/650): 0;break;} Packedpos = (Datas[0] >> 3) & 0x0f;pos + Packedsize[packedpos] + 1;framecount++;} Duration + = Framecount * 20;//frame number *20} finally {if (randomaccessfile! = null) {Randomaccessfile.close ();}} return duration;}}
Good two of the most critical classes come up, and then how to call it?
@Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: Recordpath = Constants.cache_sdcard_path + system.currenttimemillis () + ". Amr";d lg.show (Recordpath); break;case MotionEvent.ACTION_UP:dlg.dismiss (); break;} return false;}
The DLG is a recording dialog box defined above:
Private Recorderdialog Dlg; Recording dialog Box DLG = new Recorderdialog (this); Instantiating in activity
One of the r.drawable.bg_recorder_value_xxxx is a series of pictures that change in the sound of a picture.
Recording completion will generate an. amr file under Recordpath and send it out.