android 音頻錄製

我們通常可以通過以下步驟在Android中錄製音頻或視頻

(1)  創建android.media.MediaRecorder的一個實例。

(2)  創建android.content.ContentValues的一個實例,並添加TITLE、TIMESTAMP及重要的MIME_TYPE等屬性。

(3)  使用android.content.ContentResolver爲數據創建一個目標文件路徑。

(4)  要在視圖界面上設置預覽顯示,可以使用MediaRecorder.setPreviewDisplay()方法。

(5)  使用MediaRecorder.setAudioSource()設置音頻來源。

(6)  使用MediaRecorder.setOutputFormat()設置輸出文件格式。

(7)  使用MediaRecorder.setAudioEncoder()設置音頻編碼。

(8)  使用prepare()和start()準備和開始錄製。

(9)  使用stop()和release()正常停止和清除錄製過程。

代碼如下:

------------------------RecordingActivity--------------------------

package cn.com.pan;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class RecordingActivity extends Activity {
 MediaRecorder mRecorder;
 File mSampleFile = null;
 static final String SAMPLE_PREFIX ="recording";
 static final String SAMPLE_EXTENSION =".mp3";
 private static final String TAG ="RecordingActivity";
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  this.mRecorder = newMediaRecorder();
  Button startRecording =(Button) this.findViewById(R.id.startRecording);
  Button stopRecording = (Button)this.findViewById(R.id.stopRecording);
  startRecording.setOnClickListener(newView.OnClickListener() {
   @Override
   public voidonClick(View v) {
    //TODO Auto-generated method stub
    startRecording();
   }
  });
  stopRecording.setOnClickListener(newView.OnClickListener() {
   @Override
   public voidonClick(View v) {
    //TODO Auto-generated method stub
    stopRecording();
    addToDB();
   }
  });
 }
 protected void addToDB() {
  ContentValues values = newContentValues(3);
  long current =System.currentTimeMillis();
  values.put(MediaColumns.TITLE,"test_audio");
  values.put(MediaColumns.DATE_ADDED,(int) (current / 1000));
  values.put(MediaColumns.MIME_TYPE,"audio/mp3");
  values.put(MediaColumns.DATA,mSampleFile.getAbsolutePath());
  ContentResolver contentResolver= getContentResolver();
  Uri base =MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  Uri newUri =contentResolver.insert(base, values);
  sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
 }
 protected void startRecording() {
  this.mRecorder = newMediaRecorder();
  this.mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  this.mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  this.mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  this.mRecorder.setOutputFile(this.mSampleFile.getAbsolutePath());
  try {
   this.mRecorder.prepare();
  } catch (IllegalStateExceptione1) {
   // TODOAuto-generated catch block
   e1.printStackTrace();
  } catch (IOException e1){
   // TODOAuto-generated catch block
   e1.printStackTrace();
  }
  this.mRecorder.start();
  if (this.mSampleFile == null){
   FilesampleDir = Environment.getExternalStorageDirectory();
   try {
    this.mSampleFile= File.createTempFile(
      RecordingActivity.SAMPLE_PREFIX,
      RecordingActivity.SAMPLE_EXTENSION,sampleDir);
   } catch(IOException e) {
    //TODO Auto-generated catch block
    Log.e(TAG,"sdcard access error");
    return;
   }
  }
 }
 protected void stopRecording() {
  this.mRecorder.stop();
  this.mRecorder.release();
 }
}

--------------------------------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">
 <Buttonandroid:id="@+id/startRecording"android:layout_width="fill_parent"
  android:layout_height="wrap_content"android:text="start"></Button>
 <Buttonandroid:id="@+id/stopRecording"android:layout_width="fill_parent"
  android:layout_height="wrap_content"android:text="stop"></Button>
</LinearLayout>

---------------------------------AndroidManifest.xml---------------------------

<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="cn.com.pan" android:versionCode="1"android:versionName="1.0">
 <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  <activityandroid:name=".RecordingActivity"android:label="@string/app_name">
   <intent-filter>
    <actionandroid:name="android.intent.action.MAIN" />
    <categoryandroid:name="android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
 </application>
 <uses-permissionandroid:name="android.permission.RECORD_AUDIO"></uses-permission>
 <uses-sdkandroid:minSdkVersion="3" />
</manifest>

如果在計算機的音頻系統中播放音樂,AndroidEmulator將選取該音樂並直接錄製到音頻緩衝區中(並不是從麥克風進行實際錄製)。然後可以輕鬆測試該音頻文件,方法是打開AndroidMusic Player並選擇Playlist>RecentlyAdded。播放器就會播放剛錄製的音頻文件了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章