【发布时间】:2012-03-26 00:28:50
【问题描述】:
我可以通过 android 的 MediaRecord 制作 wav 文件...但我有兴趣将音频数据放入缓冲区并逐字节读取...我必须通过 TCP 通道逐字节发送音频块... .任何人都可以帮助我...提前谢谢,
来自奥地利的szaman
【问题讨论】:
标签: android
我可以通过 android 的 MediaRecord 制作 wav 文件...但我有兴趣将音频数据放入缓冲区并逐字节读取...我必须通过 TCP 通道逐字节发送音频块... .任何人都可以帮助我...提前谢谢,
来自奥地利的szaman
【问题讨论】:
标签: android
您可以使用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。希望对您有所帮助。
【讨论】: