Android仿微信錄制語音功能
本文實例為大家分享了Android仿微信錄制語音的具體代碼,供大家參考,具體內(nèi)容如下
前言
我把錄音分成了兩部分
1.UI界面,彈窗讀秒
2.一個類(包含開始、停止、創(chuàng)建文件名功能)
第一部分
由于6.0權(quán)限問題,點擊按鈕申請權(quán)限通過則彈窗,如何申請權(quán)限
彈窗布局popw_record.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="260dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:background="@drawable/take_phone" android:orientation="vertical"> <ImageView android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="10dp" android:src="@mipmap/guanbi" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/luyin" /> <Chronometer android:id="@+id/timer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:format="%s" /> <TextView android:id="@+id/startRecord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/playrecord" android:layout_marginTop="20dp" android:background="@color/background" android:padding="10dp" /> </LinearLayout> </RelativeLayout> </LinearLayout>
彈彈彈
/** * 開始錄音 */ private void showPopup() { final View contentView = LayoutInflater.from(Orderdeatil.this).inflate(R.layout.popw_record, null); mPopWindow = new PopupWindow(contentView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true); mPopWindow.setContentView(contentView); TextView startRe = (TextView) contentView.findViewById(R.id.startRecord); startRe.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP://松開事件發(fā)生后執(zhí)行代碼的區(qū)域 if (mPopWindow != null) { mPopWindow.dismiss(); sr.stopRecording(); } break; case MotionEvent.ACTION_DOWN://按住事件發(fā)生后執(zhí)行代碼的區(qū)域 Chronometer timer = (Chronometer) contentView.findViewById(R.id.timer); timer.setBase(SystemClock.elapsedRealtime());//計時器清零 timer.start();//開始錄音的提示 sr.startRecording(); break; case MotionEvent.ACTION_CANCEL: if (mPopWindow != null) { mPopWindow.dismiss(); sr.stopRecording();//停止錄音 } break; default: break; } return true; } }); ImageView close = (ImageView) contentView.findViewById(R.id.close); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPopWindow.dismiss(); } }); mPopWindow.setTouchable(true); mPopWindow.setFocusable(true); mPopWindow.setBackgroundDrawable(new BitmapDrawable()); mPopWindow.setOutsideTouchable(true); mPopWindow.setTouchInterceptor(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { mPopWindow.dismiss(); return true; } return false; } }); View rootview = LayoutInflater.from(Orderdeatil.this).inflate(R.layout.activity_orderdeatil, null); mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0); }
第二部分 工具類
class SoundRecorder { public void startRecording() { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mRecorder.setOutputFile(newFileName()); try { // 準備好開始錄音 mRecorder.prepare(); mRecorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void stopRecording() { if (mRecorder != null) { //added by ouyang start try { //下面三個參數(shù)必須加,不加的話會奔潰,在mediarecorder.stop(); //報錯為:RuntimeException:stop failed mRecorder.setOnErrorListener(null); mRecorder.setOnInfoListener(null); mRecorder.setPreviewDisplay(null); mRecorder.stop(); } catch (IllegalStateException e) { // TODO: handle exception Log.i("Exception", Log.getStackTraceString(e)); } catch (RuntimeException e) { // TODO: handle exception Log.i("Exception", Log.getStackTraceString(e)); } catch (Exception e) { // TODO: handle exception Log.i("Exception", Log.getStackTraceString(e)); } //added by ouyang end mRecorder.release(); mRecorder = null; upRecord(); } } public String newFileName() { mFileName = Environment.getExternalStorageDirectory() .getAbsolutePath(); String s = new SimpleDateFormat("yyyy-MM-dd hhmmss") .format(new Date()); return mFileName += "/rcd_" + s + ".mp3"; } }
這是從我代碼中擇出來的,加上權(quán)限應該是可以的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)用代碼簡單安裝和卸載APK的方法
這篇文章主要介紹了Android實現(xiàn)用代碼簡單安裝和卸載APK的方法,涉及Android針對APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android開發(fā)實現(xiàn)自定義新聞加載頁面功能實例
這篇文章主要介紹了Android開發(fā)實現(xiàn)自定義新聞加載頁面功能,結(jié)合具體實例形式分析了Android界面加載及頁面布局相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android自定義View之組合控件實現(xiàn)類似電商app頂部欄
這篇文章主要為大家詳細介紹了Android自定義View之組合控件,實現(xiàn)類似電商app頂部欄的相關(guān)資料,具有參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android 實現(xiàn)銀聯(lián)刷卡機消費后手動簽名的功能(示例代碼)
在一些商場購物時,不需要用筆在銀聯(lián)機上簽名了,直接用手指觸摸實現(xiàn)消費簽名,非常方便,下面小編給大家分享Android 實現(xiàn)銀聯(lián)刷卡機消費后手動簽名的功能,需要的朋友參考下吧2017-12-12Android常用布局(FrameLayout、LinearLayout、RelativeLayout)詳解
這篇文章主要為大家詳細介紹了Android常用布局FrameLayout、LinearLayout、RelativeLayout,感興趣的小伙伴們可以參考一下2016-06-06Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例
這篇文章主要介紹了Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能,結(jié)合簡單實例形式分析了Android使用CheckBox控件的布局與功能實現(xiàn)技巧,需要的朋友可以參考下2017-07-07