[Android]實現點擊持續錄音,鬆開結束錄音,並實現隨著分貝的大小改變圖片

來源:互聯網
上載者:User

標籤:als   rate   add   tle   tca   pat   隱藏   sed   ica   

顯示錄音大小的DIALOG實現

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) {    //根據錄音的分貝改變大小        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_feedback004);        }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載入的XML頁面實現

<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_parent"      android:layout_height="90dp"      android:layout_marginTop="5dp"      android:id="@+id/bianhua">    <ImageView        android:id="@+id/vioce_show"        android:layout_width="wrap_content"        android:layout_height="50dp"        android:layout_alignParentBottom="true"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:src="@drawable/voicesearch_feedback001" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="正在錄音中"         android:gravity="center"        /></RelativeLayout>  

主介面的試下

  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實現

    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(!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {                    Toast.makeText(ChatActivity.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{                        //建立檔案,準備錄製                        soundfile.createNewFile();                        mmedio.prepare();                    }catch(IllegalStateException  e) {                        e.printStackTrace();                    }                    //開始錄製                    thend.start();                    mmedio.start();                    speak.setText("鬆開停止錄音");                    viocedomio.showRecordingDialog();   //顯示錄音的大小                    final   Handler mHandler = new Handler() {                          @Override                              public void handleMessage(Message msg) {                                 viocedomio.ChangeRecordingDialog(Double.parseDouble(msg.obj.toString()));                            }                        };                    new Thread(new Runnable() {   //啟動線程根據改變錄音顯示大小                        @Override                        public void run() {                            // TODO Auto-generated method stub                            while(mmedio!=null) {                                double ratio = (double)mmedio.getMaxAmplitude() ;                                double db = 0;// 分貝                                if (ratio > 1)                                    db = 20 * Math.log10(ratio);                               // Log.d(TAG,分貝值:+db);                               // mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);                                   Message msg=new Message();                                msg.obj=db;                                mHandler.sendMessage(msg);                            }                        }                    }).start();                }catch(Exception e) {                    e.printStackTrace();                }            }            if(event.getAction()==MotionEvent.ACTION_UP) {   //如果是鬆開                if(soundfile != null && soundfile.exists()) {  //停止錄音                    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("按住請講話!!!");                    String content="";                    if(content.isEmpty()) {                        viocedomio.HideRecordingDialog();  //隱藏錄音標誌                        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(); //當有新訊息時,重新整理listview中的顯示                        msgListView.setSelection(msglist.size()-1); //將listview定位到最後一行                        }                    }                }            return true;        }    });

[Android]實現點擊持續錄音,鬆開結束錄音,並實現隨著分貝的大小改變圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.