2011-06-15 98 views

回答

0

您可以使用AudioRecord来读取字节的音频数据字节,这里是一些示例代码。

// calculate the minimum buffer 
int minBuffer = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT); 

// initialise audio recorder and start recording 
AudioRecord mRec = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, 
       CHANNEL_CONFIG, AUDIO_FORMAT, 
       minBuffer); 
mRec.startRecording(); 
byte[] pktBuf = new byte[pktSizeByte]; 
boolean ok; 
// now you can start reading the bytes from the AudioRecord 
while (!finished) { 
    // fill the pktBuf 
    readFully(pktBuf, 0, pktBuf.length); 
    // make a copy 
    byte[] pkt = Arrays.copyOf(pktBuf, pktBuf.length); 
    // do anything with the byte[] ... 
} 

由于到read()单个呼叫可能无法获得足够的数据来填充byte[] pktBuf,我们可能需要多次读取填充缓冲区。在这种情况下,我使用了一个辅助函数“readFully”来确保填充缓冲区。根据你想与你的代码做什么,不同的策略可用于...

/* fill the byte[] with recorded audio data */ 
private void readFully(byte[] data, int off, int length) { 
    int read; 
    while (length > 0) { 
     read = mRec.read(data, off, length); 
     length -= read; 
     off += read; 
    } 
} 

记得拨打mRec.stop()完成后停止AudioRecorder。希望有所帮助。