我想访问在MediaRecorder
录制过程中正在录制的音频字节,以便我可以使用 UdpClient 将它们发送到服务器应用程序。
我可以AudioRecord
通过执行以下操作来做到这一点(注意while(true)
循环)
endRecording = false;
isRecording = true;
audioBuffer = new Byte[1024];
audioRecord = new AudioRecord (
// Hardware source of recording.
AudioSource.Mic,
// Frequency
11025,
// Mono or stereo
ChannelIn.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length
);
audioRecord.StartRecording ();
while (true) {
if (endRecording) {
endRecording = false;
break;
}
try {
// Keep reading the buffer while there is audio input.
int numBytes = await audioRecord.ReadAsync (audioBuffer, 0, audioBuffer.Length);
//Send the audio data with the DataReceived event where it gets send over UdpClient in the Activity code
byte[] encoded = audioBuffer; //TODO: encode audio data, for now just stick with regular PCM audio
DataReceived(encoded);
} catch (Exception ex) {
Console.Out.WriteLine (ex.Message);
break;
}
}
audioRecord.Stop ();
audioRecord.Release ();
isRecording = false;
但我不确定如何取出字节,MediaRecorder
所以我可以做类似的事情。我看到的大多数示例仅在录制完成后使用文件,例如来自此处和此处的以下示例代码。
我不想在开始发送之前等待完整的录音。我不需要MediaRecorder
记录文件,只需让我访问字节即可。但是可以选择同时写入文件和发送字节会很好。有没有办法做到这一点,也许通过使用ParcelFileDescriptor
或其他方式?