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

Android 使用 MediaRecorder 錄音與播放

編輯:關於 Android 程式設計

上一篇介紹了以意圖(Intent)錄音的方式,本文則改用 MediaRecorder 類別來錄音,相較之下能提供更高的彈性與控制力,例如自訂音訊來源、輸出格式與編碼器。

完成後的效果如下:

版面配置

畫面包含一個狀態文字與四個按鈕,分別負責開始錄音、停止錄音、播放錄音與結束:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/statusTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Status"
        android:gravity="center_horizontal" />
    <Button
        android:id="@+id/button_startRecording"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開始錄音" />
    <Button
        android:id="@+id/button_stopRecording"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止錄音" />
    <Button
        android:id="@+id/button_playRecording"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放錄音" />
    <Button
        android:id="@+id/button_finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="完成" />
</LinearLayout>

錄音與播放邏輯

按下開始錄音時,建立 MediaRecorder 並依序設定音訊來源、輸出格式與編碼器,再指定輸出檔案後即可開始。停止時則釋放 MediaRecorder,並以 MediaPlayer 載入錄音檔以便播放。

public class MainActivity extends Activity implements OnClickListener, OnCompletionListener {
    private TextView statusTextView;
    private Button button_startRecording, button_stopRecording, button_playRecording, button_finish;
    private MediaRecorder recorder;
    private MediaPlayer player;
    private File audioFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        statusTextView = (TextView) findViewById(R.id.statusTextView);
        statusTextView.setText("Ready");
        button_startRecording = (Button) findViewById(R.id.button_startRecording);
        button_playRecording = (Button) findViewById(R.id.button_playRecording);
        button_stopRecording = (Button) findViewById(R.id.button_stopRecording);
        button_finish = (Button) findViewById(R.id.button_finish);
        button_startRecording.setOnClickListener(this);
        button_playRecording.setOnClickListener(this);
        button_stopRecording.setOnClickListener(this);
        button_finish.setOnClickListener(this);
        button_playRecording.setEnabled(false);
        button_stopRecording.setEnabled(false);
        player = new MediaPlayer();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_startRecording:
                try {
                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    File path = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/files/");
                    path.mkdir();
                    audioFile = File.createTempFile("recording", ".3gp", path);
                    recorder.setOutputFile(audioFile.getAbsolutePath());
                    recorder.prepare();
                    recorder.start();
                    statusTextView.setText("Recording");
                    button_playRecording.setEnabled(false);
                    button_stopRecording.setEnabled(true);
                    button_startRecording.setEnabled(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_playRecording:
                player.start();
                statusTextView.setText("playing");
                button_playRecording.setEnabled(false);
                button_stopRecording.setEnabled(false);
                button_startRecording.setEnabled(false);
                break;
            case R.id.button_stopRecording:
                try {
                    recorder.stop();
                    recorder.release();
                    player.setOnCompletionListener(this);
                    player.setDataSource(audioFile.getAbsolutePath());
                    player.prepare();
                    statusTextView.setText("Ready to Play!!");
                    button_playRecording.setEnabled(true);
                    button_stopRecording.setEnabled(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_finish:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        button_playRecording.setEnabled(true);
        button_startRecording.setEnabled(true);
        button_stopRecording.setEnabled(false);
        statusTextView.setText("Ready...");
    }
}

透過 MediaRecorder 搭配 MediaPlayer,就能輕鬆完成錄音與播放的完整流程。別忘了在 AndroidManifest.xml 中加上錄音(RECORD_AUDIO)與外部儲存寫入權限。

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