AudioRecord 如何保存文件(PCM.WAV)?
static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/reverseme.pcm");
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
short[] buffer = new short[recBufSize];
audioRecord.startRecording();
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
for(int i = 0; i < bufferReadResult;i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
dos.flush();
dos.close();
但是,打开保存文件(reverseme.pcm),无法播放。 帮帮我,谢谢。
static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/reverseme.pcm");
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
short[] buffer = new short[recBufSize];
audioRecord.startRecording();
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
for(int i = 0; i < bufferReadResult;i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
dos.flush();
dos.close();
but, open the save file(reverseme.pcm), can not play.
Help me, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你犯了一个大错误:当你设置
AudioRecord.getMinBufferSize
的参数时,你选择channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO
,但是你可以查找应该设置channelConfig的字典描述音频通道的配置。请参阅CHANNEL_IN_MONO
和CHANNEL_IN_STEREO
。You are making a big mistake : when you set
AudioRecord.getMinBufferSize
's parameters,you choosechannelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO
,but you can look up dictionary that is should set channelConfig describes the configuration of the audio channels. SeeCHANNEL_IN_MONO
andCHANNEL_IN_STEREO
.