Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

安卓開發實作:以 MediaRecorder 實現錄音與播放

編輯:關於 Android 開發

錄音是許多多媒體應用的基礎功能。Android 提供了 MediaRecorder 類別,能輕鬆將音訊錄製為 AMR 等格式的檔案,再搭配 ListView 列出並播放。以下是一個完整的錄音播放範例。

Activity 中包含四個控制按鈕(錄製、停止、播放、刪除),並以 ListView 列出指定資料夾中的所有 AMR 音訊檔。初始化時先將停止、播放、刪除按鈕設為不可用:

 public class EX07_11 extends Activity { private ImageButton myButton1; private ImageButton myButton2; private ImageButton myButton3; private ImageButton myButton4; private ListView myListView1; private String strTempFile = "ex07_11_"; private File myRecAudioFile; private File myRecAudioDir;// 得到 SD 卡 path private File myPlayFile; private MediaRecorder mMediaRecorder01; private ArrayList<String> recordFiles; private ArrayAdapter<String> adapter; private TextView myTextView1; private boolean sdCardExit; private boolean isStopRecord; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1 = (ImageButton) findViewById(R.id.ImageButton01); myButton2 = (ImageButton) findViewById(R.id.ImageButton02); myButton3 = (ImageButton) findViewById(R.id.ImageButton03); myButton4 = (ImageButton) findViewById(R.id.ImageButton04); myListView1 = (ListView) findViewById(R.id.ListView01); myTextView1 = (TextView) findViewById(R.id.TextView01); myButton2.setEnabled(false); myButton3.setEnabled(false); myButton4.setEnabled(false);

版面檔 main.xml 以 LinearLayout 排列四個 ImageButton 與一個 ListView:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/record"> </ImageButton> <ImageButton android:id="@+id/ImageButton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop"> </ImageButton> <ImageButton android:id="@+id/ImageButton03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play"> </ImageButton> <ImageButton android:id="@+id/ImageButton04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/delete"> </ImageButton> </LinearLayout> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/black"> </TextView> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/black"> </ListView> </LinearLayout>

ListView 的項目版型 my_simple_list_item.xml 使用 CheckedTextView:

 <?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/myCheckedTextView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@drawable/white" />

錄音的核心在於設定 MediaRecorder 的輸出格式與音源,例如 setOutputFormat、setAudioEncoder、setOutputFile 等,開始與停止分別呼叫 start() 與 stop()。別忘了在 manifest 中加入錄音權限:

 <uses-permission android:name="android.permission.RECORD_AUDIO" />

如此便能完成一個具備錄製、播放、刪除功能的簡單錄音機。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved