編輯:Android 技術基礎
Android 的 MediaRecorder 類別可以相對簡單地完成音訊錄製。不過要注意,模擬器對錄音支援有限,實際測試建議使用真機。
先將 record.png 與 stop.png 複製到 res/drawable-hdpi。
activity_main.xml 可放入兩個 ImageButton,分別代表開始錄音與停止錄音。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/record" />
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</LinearLayout>
在 MainActivity 中,先檢查外部儲存是否可用,再建立檔案路徑。按下錄音鍵時,設定 MIC、THREE_GPP、AMR_NB,然後 prepare 與 start。
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(path);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
mRecorder.start();
停止錄音時,則要停止並釋放資源。
mRecorder.stop();
mRecorder.release();
mRecorder = null;
點擊錄音按鈕後,系統會開始收音,停止後會產生 3gp 格式的音訊檔。

HTTP請求頭與響應頭完全指南:概念對照與Servlet實戰
本節導讀 上一節我們已經對 Android 的網路編程有了初步認識,也理清了 HTTP 的基本概念。本節要深入探討的是 HTTP 的請求頭(Request Header)
第60章、把資料寫進記憶卡再讀回來(從零開始學Android)
以檔案方式保存資料是最傳統、也最容易理解的做法。好消息是:Android 上的檔案讀寫寫法與 Java SE 的 I/O 幾乎完全一致,File、FileOutputSt
搞定 ListView 裡 CheckBox 的經典錯位問題
本節引言: 只要你在自定義的 ListView item 裡放了一個 CheckBox,當列表項超過一屏,幾乎一定會撞上這個經典坑——滾動時勾選狀態亂竄。下面我們先剖析它
Storage Access Framework 入門:用 Document Provider 讀取與管理檔案
從 ContentProvider 到檔案瀏覽器ContentProvider 可用來共用資料,而 Android 4.4 後加入的 Storage Access Fra