[Android] Achieve click to continue recording, release the end of the recording, and realize the size of the decibel to change the picture

Source: Internet
Author: User

dialog implementation of display recording size

public class Viocedomio {private Context mcontext;    Private Alertdialog Dialog;      Private Alertdialog.builder Adialogbuile;    Private ImageView vioce_show;    Public Viocedomio (Context mcontext) {this.mcontext=mcontext; } public void Showrecordingdialog () {adialogbuile = new Alertdialog.builder (Mcontext, R.style.theme_audiodialog          );          Layoutinflater inflater = Layoutinflater.from (Mcontext);          View view=inflater.inflate (r.layout.voice_chage,null);        vioce_show= (ImageView) View.findviewbyid (r.id.vioce_show);        Adialogbuile.setview (view);        Dialog = Adialogbuile.create ();        Dialog.show (); Dialog.    GetWindow (). setlayout (500, 450); } public void Hiderecordingdialog () {if (Dialog!=null && dialog.isshowing ()) {Dialog.dismiss            ();        Dialog=null;            }} public void Changerecordingdialog (double db) {//change size according to recorded decibel if (db>=1 && db<=18) { VIoce_show.setimageresource (r.drawable.voicesearch_feedback001);        }else if (db>=19 && db<=37) {vioce_show.setimageresource (r.drawable.voicesearch_feedback002); }else if (db>=19 && db<=37) {Vioce_show.setimageresource (r.drawable.voicesearch_feedback003)        ; } else if (db>=38 && db<=56) {Vioce_show.setimageresource (r.drawable.voicesearch_feedback0        04);        }else if (db>=57 && db<=75) {vioce_show.setimageresource (r.drawable.voicesearch_feedback005); }else if (db>=76 && db<=94) {Vioce_show.setimageresource (r.drawable.voicesearch_feedback006)        ;        }else if (db>=95 && db<=100) {vioce_show.setimageresource (r.drawable.voicesearch_feedback007); }    }}

dialog loaded XML page implementation

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http// Schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_par Ent "android:layout_height=" 90DP "android:layout_margintop=" 5DP "android:id=" @+id/bianhua "> <image        View android:id= "@+id/vioce_show" android:layout_width= "wrap_content" android:layout_height= "50DP" Android:layout_alignparentbottom= "true" android:layout_alignparenttop= "true" Android:layout_centerhoriz Ontal= "true" android:src= "@drawable/voicesearch_feedback001"/> <textview android:id= "@+id/textview 1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignparen        Tbottom= "true" android:layout_centerhorizontal= "true" android:text= "is recording" android:gravity= "Center"   /></relativelayout>

The main interface of the test

  1. Activoti.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Button        android:id="@+id/speak"        android:layout_width="wrap_content"        android:layout_height="42dp"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:paddingLeft="10dp"        android:layout_weight="0.62"        android:background="@drawable/search_ba"        android:drawableLeft="@drawable/speak_1"         android:gravity="center"        android:text="按住请讲话!!!"        android:textSize="15sp"         android:singleLine="true"        android:visibility="gone"          /></LinearLayout >
  2. Mainactiovity.java implementation

    protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Requestwindowfeature (Window.feature_no_title);    Setcontentview (R.layout.activoti);    Send= (Button) Findviewbyid (r.id.send);    Viocedomio=new Viocedomio (this);            Speak.setontouchlistener (New View.ontouchlistener () {public boolean OnTouch (View V, motionevent event) {            TODO auto-generated Method Stub countingthreadextends thend=new countingthreadextends (); if (Event.getaction () ==motionevent.action_down) {//If the button is in the pressed state//recording if (! Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {Toast.maketext (Ch                    Atactivity.this, "SD not", Toast.length_short). Show ();                return false;                } String sound_path=time_path+ "Sound.amr"; try{soundfile=new File (Environment.getexternalstoragedirectory (). GetcanonicalfiLe () + "/bishevoice/" +sound_path);                    LOG.D ("Send_filepath", Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/bishevoice/" +sound_path);                    Mmedio=new Mediarecorder ();                    Mmedio.setaudiosource (MediaRecorder.AudioSource.MIC);                    Mmedio.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);                    Mmedio.setoutputfile (Soundfile.getabsolutepath ());                    Mmedio.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);                        try{//create file, prepare to record soundfile.createnewfile ();                    Mmedio.prepare ();                    }catch (IllegalStateException e) {e.printstacktrace ();                    }//Start recording thend.start ();                    Mmedio.start ();                    Speak.settext ("Release Stop Recording");   Viocedomio.showrecordingdialog ();               Show the size of the recording     Final Handler Mhandler = new Handler () {@Override public voi D handlemessage (Message msg) {Viocedomio.                            Changerecordingdialog (Double.parsedouble (msg.obj.toString ()));                    }                        };                             The new Thread (new Runnable () {//Startup thread varies according to the recording display size @Override public void run () {                                TODO auto-generated Method Stub while (Mmedio!=null) {                                Double ratio = (double) mmedio.getmaxamplitude (); Double db = 0;//db if (ratio > 1) db = $ * Math.log                               Ten (ratio);                               LOG.D (TAG, decibel value: +db);                                   Mhandler.postdelayed (Mupdatemicstatustimer, SPACE); Message msg=newMessage ();                                msg.obj=db;                            Mhandler.sendmessage (msg);                }}). Start ();                }catch (Exception e) {e.printstacktrace (); }} if (Event.getaction () ==motionevent.action_up) {//If it is released if (soundfile! = null &am                        p;& soundfile.exists ()) {//Stop recording try {mmedio.setonerrorlistener (null);                        Mmedio.setoninfolistener (NULL);                        Mmedio.setpreviewdisplay (NULL);                        Mmedio.stop ();                        Thend.interrupt ();                    Timetask.cancel ();                    }catch (Exception e) {log.i ("Exception", Log.getstacktracestring (e));                    } mmedio.release ();                    Mmedio=null; Speak.settext ("Hold on Please speak!!!"                    ");                    String content= ""; if (Content.isempty ()) {Viocedomio.  Hiderecordingdialog (); Hidden recording flag msg msg=new msg (content,msg.type_sent,msg.type_sent_voice,integer.tostring (time_int), File_                        path);                        Msg.setchattype (0);                        Msglist.add (msg); Adapter.notifydatasetchanged (); When there is a new message, refresh the display in the ListView msglistview.setselection (Msglist.size ()-1);        Position the ListView to the last line}}}}: Return true; }    });

[Android] enables clicking on a continuous recording, releasing the end recording, and changing the picture with the size of the decibel

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.