0
我可以通过android的MediaRecord制作wav文件......但我有兴趣将音频数据放入缓冲区并逐字节读取...... I要发送音频块通过TCP通道明智字节....任何一个可以帮我请...在此先感谢,来自奥地利如何将音频存储到缓冲区,然后逐字节地读取它
szaman
我可以通过android的MediaRecord制作wav文件......但我有兴趣将音频数据放入缓冲区并逐字节读取...... I要发送音频块通过TCP通道明智字节....任何一个可以帮我请...在此先感谢,来自奥地利如何将音频存储到缓冲区,然后逐字节地读取它
szaman
您可以使用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
。希望有所帮助。