Android仿微信錄制語(yǔ)音功能
本文實(shí)例為大家分享了Android仿微信錄制語(yǔ)音的具體代碼,供大家參考,具體內(nèi)容如下
前言
我把錄音分成了兩部分
1.UI界面,彈窗讀秒
2.一個(gè)類(lèi)(包含開(kāi)始、停止、創(chuàng)建文件名功能)
第一部分
由于6.0權(quán)限問(wèn)題,點(diǎn)擊按鈕申請(qǐng)權(quán)限通過(guò)則彈窗,如何申請(qǐng)權(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>
彈彈彈
/** * 開(kāi)始錄音 */ 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://松開(kāi)事件發(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());//計(jì)時(shí)器清零 timer.start();//開(kāi)始錄音的提示 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); }
第二部分 工具類(lèi)
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 { // 準(zhǔn)備好開(kāi)始錄音 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 { //下面三個(gè)參數(shù)必須加,不加的話(huà)會(huì)奔潰,在mediarecorder.stop(); //報(bào)錯(cuò)為: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"; } }
這是從我代碼中擇出來(lái)的,加上權(quán)限應(yīng)該是可以的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android6.0指紋識(shí)別開(kāi)發(fā)實(shí)例詳解
這篇文章主要介紹了Android6.0指紋識(shí)別開(kāi)發(fā)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法
這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法,涉及Android針對(duì)APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細(xì)介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能,結(jié)合具體實(shí)例形式分析了Android界面加載及頁(yè)面布局相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android自定義View之組合控件實(shí)現(xiàn)類(lèi)似電商app頂部欄
這篇文章主要為大家詳細(xì)介紹了Android自定義View之組合控件,實(shí)現(xiàn)類(lèi)似電商app頂部欄的相關(guān)資料,具有參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android中FontMetrics的幾個(gè)屬性全面講解
下面小編就為大家?guī)?lái)一篇Android中FontMetrics的幾個(gè)屬性全面講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能(示例代碼)
在一些商場(chǎng)購(gòu)物時(shí),不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實(shí)現(xiàn)消費(fèi)簽名,非常方便,下面小編給大家分享Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能,需要的朋友參考下吧2017-12-12Android的App啟動(dòng)時(shí)白屏的問(wèn)題解決辦法
這篇文章主要介紹了Android的App啟動(dòng)時(shí)白屏的問(wèn)題相關(guān)資料,在A(yíng)pp啟動(dòng)的第一次的時(shí)候白屏?xí)欢螘r(shí)間,這里提供了解決辦法,需要的朋友可以參考下2017-08-08Android常用布局(FrameLayout、LinearLayout、RelativeLayout)詳解
這篇文章主要為大家詳細(xì)介紹了Android常用布局FrameLayout、LinearLayout、RelativeLayout,感興趣的小伙伴們可以參考一下2016-06-06Android開(kāi)發(fā)之CheckBox的簡(jiǎn)單使用與監(jiān)聽(tīng)功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之CheckBox的簡(jiǎn)單使用與監(jiān)聽(tīng)功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了Android使用CheckBox控件的布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07