对于我的应用程序,我需要在 Android 手机上录制来自 MIC 的音频,并通过 TCP 将其发送到需要播放的另一部 Android 手机。
我在用AudioRecord
and AudioTrack
班级。这对于文件非常有效 - 使用以下命令将音频写入文件DataOutputStream
,并使用它读取DataInputStream
.
但是,如果我从套接字而不是文件获取相同的流,并尝试写入它,则会出现异常。
我不知道可能出了什么问题。任何帮助将不胜感激。
编辑:即使我尝试使用更大的缓冲区大小(65535 字节,160000 字节),问题也是一样的。
这是代码:
录音机:
int bufferSize = AudioRecord.getMinBufferSize(11025, , AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
byte[] tempBuffer = new byte[bufferSize];
recordInstance.startRecording();
while (/*isRecording*/) {
bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
dataOutputStreamInstance.write(tempBuffer);
}
上面的DataOutputStream的获取方式为:
BufferedOutputStream buff = new BufferedOutputStream(out1); //out1 is the socket's outputStream
DataOutputStream dataOutputStreamInstance = new DataOutputStream (buff);
您能否看一下,让我知道我在这里可能做错了什么?
Thanks,