2012-07-11 64 views
4

我有一個LG電話與版本2.3.3。我需要在耳機插孔中連接麥克風,因爲我想創建一個讀取聲音樣本的應用程序。如何在Android中使用audio api錄製音頻?

如何以編程方式讀取示例?

+0

你應該分裂這個問題分成兩個不同的人。你應該在[android.stackexchange.com](http://android.stackexchange.com/)中詢問的第一部分! – Zuul 2012-07-11 21:30:37

回答

1

2)如何以編程方式讀取樣本?

,我知道,在Android中您可以使用此兩類之一錄製聲音:

  • MediaRecorder Class

    用於錄製音頻和視頻。記錄控制是基於一個簡單的狀態機

  • AudioRecord class

    的AudioRecord類管理Java應用程序的音頻資源記錄從平臺的音頻輸入硬件音頻上。這是通過「拉動」(讀取)來自AudioRecord對象的數據來實現的。應用程序負責使用以下三種方法之一及時輪詢AudioRecord對象:read(byte [],int,int),read(short [],int,int)或read(ByteBuffer,int)。選擇使用哪種方法將基於AudioRecord用戶最方便的音頻數據存儲格式。

PS:按照上面這個鏈接來閱讀和理解最適合您的需求的。


1)你推薦哪麥克風?

正如我在放在你的問題的評論中提到,這個在這裏得到off-topic上計算器,但出於完整性的目的:

6

如果你確實想讀取AudioSamples,我會建議指示您使用AudioRecord而不是MediaRecorder,因爲它可以讓您更好地控制AudioSamples ...爲了您可以使用以下代碼,AudioCapturer是我用於從AudioRecord對象獲取樣本的包裝類.IAudioReceiver是一個接口,它具有處理音頻數據的方法。

public class AudioCapturer implements Runnable { 

    private AudioRecord audioRecorder = null; 
    private int bufferSize; 
    private int samplePerSec = 16000; 
    private String LOG_TAG = "AudioCapturer"; 
    private Thread thread = null; 

    private boolean isRecording; 
    private static AudioCapturer audioCapturer; 

    private IAudioReceiver iAudioReceiver; 

    private AudioCapturer(IAudioReceiver audioReceiver) { 
     this.iAudioReceiver = audioReceiver; 
    } 

    public static AudioCapturer getInstance(IAudioReceiver audioReceiver) { 
     if (audioCapturer == null) { 
      audioCapturer = new AudioCapturer(audioReceiver); 
     } 
     return audioCapturer; 
    } 

    public void start() { 

     bufferSize = AudioRecord.getMinBufferSize(samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); 

     if (bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR) { 

      audioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, this.samplePerSec, AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, this.bufferSize * 10); // bufferSize 
                      // 10x 

      if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) { 
       Log.i(LOG_TAG, "Audio Recorder created"); 


       audioRecorder.startRecording(); 
       isRecording = true; 
       thread = new Thread(this); 
       thread.start(); 

      } else { 
       Log.e(LOG_TAG, "Unable to create AudioRecord instance"); 
      } 

     } else { 
      Log.e(LOG_TAG, "Unable to get minimum buffer size"); 
     } 
    } 

    public void stop() { 
     isRecording = false; 
     if (audioRecorder != null) { 
      if (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) { 
       // System.out 
       // .println("Stopping the recorder inside AudioRecorder"); 
       audioRecorder.stop(); 
      } 
      if (audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) { 
       audioRecorder.release(); 
      } 
     } 
    } 

    public boolean isRecording() { 
     return (audioRecorder != null) ? (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) : false; 
    } 

    @Override 
    public void run() { 
     android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); 
     while (isRecording && audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) { 
      short[] tempBuf = new short[Constants.FRAME_SIZE/2]; 
      audioRecorder.read(tempBuf, 0, tempBuf.length);  
      iAudioReceiver.capturedAudioReceived(tempBuf, false); 
     } 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Object#finalize() 
    */ 
    @Override 
    protected void finalize() throws Throwable { 
     super.finalize(); 
     System.out.println("AudioCapturer finalizer"); 
     if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) { 
      audioRecorder.stop(); 
      audioRecorder.release(); 
     } 
     audioRecorder = null; 
      iAudioReceiver = null; 
     thread = null; 
    } 

} 

現在你可以從主類,你計劃使用這個類的對象,它會開始給你的音頻採樣你能處理他們的IAudioReceiver(類使用這些樣本)內..

如果你還是想用MediaRecorder,this link能對你有用,