淺入淺出Android(008):使用MediaRecorder錄音

使用MediaRecorder既能夠錄音也能夠錄像。本文先只講如何錄音。測試的系統是android4。

一、增長錄音和讀寫外部存儲的權限

修改AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.HelloWorld"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="14"/>

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

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>



二、設計界面,修改layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="錄音"
            />

    <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="運行信息:"
            />

    <Button
        android:id="@+id/start_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始錄音"
        />
    <Button
            android:id="@+id/stop_record"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中止錄音"
            />


</LinearLayout>



效果以下:


三、修改java代碼

package com.example.HelloWorld;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import android.widget.Toast;
import android.util.Log;
import java.io.File;
import java.io.IOException;


public class MyActivity extends Activity{

    Button startRecordButton;
    Button stopRecordButton;
    TextView infoText;
    MediaRecorder recorder = new MediaRecorder();
    boolean isRecording = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startRecordButton = (Button) findViewById(R.id.start_record);
        stopRecordButton = (Button) findViewById(R.id.stop_record);

        stopRecordButton.setEnabled(false);

        infoText = (TextView)findViewById(R.id.info);

        final String filePath = Environment.getExternalStorageDirectory() + "/aaaaa.amr";

        infoText.setText("保存到"+filePath);

        startRecordButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 isRecording = true;
                 try {
                     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 輸入源
                     recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //設置輸出格式
                     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //設置編碼
                     recorder.setOutputFile(filePath); //輸出文件
                     recorder.prepare();  //預備!
                     recorder.start();    //開始!
                     startRecordButton.setEnabled(false);
                     stopRecordButton.setEnabled(true);
                 } catch (Exception e) {
                     e.printStackTrace();
                     infoText.setText(e.getMessage());
                 }
             }
         });
        stopRecordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recorder.stop();
                isRecording = false;
                startRecordButton.setEnabled(true);
                stopRecordButton.setEnabled(false);
            }
        });

    }


    @Override
    protected void onDestroy() {
        if (isRecording) {
            recorder.stop();
        }
        recorder.release();
        super.onDestroy();
    }
}


錄音文件保存到變量filePath對應的路徑中。
在startRecordButton被點擊後,開始錄音。recorder是實例化的MediaRecorder,先依次設置其輸入源、輸出格式、編碼、輸出文件,而後prepare()和start()錄音就開始了。這時候禁用了startRecordButton以防用戶誤點。與此同時,默認被禁用的stopRecordButton被啓用。
在stopRecordButton被點擊後,中止錄音,調用stop()函數便可。同時禁用stopRecordButton,激活startRecordButton。

在重寫的onDestroy()方法中,會根據布爾變量isRecording判斷是否正在錄音,若在錄音在stop it,而後釋放。

初始界面:



開始錄音:


生成的音頻文件:

相關文章
相關標籤/搜索