2011-03-05 245 views
3

我想知道如何使用AudioRecord功能在Android中代替MediaRecorder錄製語音和播放。Android音頻記錄

請給我示例代碼或網址。 在此先感謝

回答

6

我想你可以通過下面的代碼原型樣

import android.media.AudioFormat; 
import android.media.AudioRecord; 
import android.media.MediaRecorder; 
import android.util.Log; 
public class AudioListener { 
    public static final int DEFAULT_SAMPLE_RATE = 8000; 
    private static final int DEFAULT_BUFFER_SIZE = 4096; 
    private static final int CALLBACK_PERIOD = 4000; // 500 msec (sample rate/callback //period) 
    private final AudioRecord recorder; 
    public AudioListener() { 
    this(DEFAULT_SAMPLE_RATE); 
    } 
    private AudioListener(int sampleRate) { 
    recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 
     sampleRate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, 
     AudioFormat.ENCODING_DEFAULT, DEFAULT_BUFFER_SIZE); 
    } 
    public void start() { 
    recorder.setPositionNotificationPeriod(CALLBACK_PERIOD); 
    recorder.setRecordPositionUpdateListener(new 
AudioRecord.OnRecordPositionUpdateListener() { 
     @Override 
     public void onMarkerReached(AudioRecord recorder) { 
     Log.e(this.getClass().getSimpleName(), "onMarkerReached 
Called"); 
     } 
     @Override 
     public void onPeriodicNotification(AudioRecord recorder) { 
     Log.e(this.getClass().getSimpleName(), "onPeriodicNotification 
Called"); 
     } 
    }); 
    recorder.startRecording(); 
    } 
+1

感謝嗨此代碼開始。可以使用活動發佈此代碼。 – 2011-03-08 14:42:14