录制WAV文件从Android的麦克风 - 问题 [英] Recording a wav file from the mic in Android - problems
问题描述
我需要能够创建一个使用Android中的麦克风WAV文件。目前,我有很多的麻烦。到目前为止,这是我的情况。我使用的micDroid项目code部位从而记录:
I need to be able to create a WAV file using the mic in Android. Currently, I'm having a lot of trouble. So far, this is my situation. I'm using parts of the micDroid project code to record thus:
//read thread
int sampleRate = 44100;
int bufferSize = AudioRecord.getMinBufferSize(sampleRate,android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT);
AudioRecord ar = new AudioRecord(AudioSource.MIC,sampleRate,android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT,bufferSize);
short[] buffer = new short[bufferSize];
ar.startRecording();
while(isRunning){
try{
int numSamples = ar.read(buffer, 0, buffer.length);
queue.put(new Sample(buffer, numSamples));
} catch (InterruptedException e){
e.printStackTrace();
}
}
//write thread
int sampleRate = 44100;
WaveWriter writer = new WaveWriter("/sdcard","recOut.wav",sampleRate,android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT);
try {
writer.createWaveFile();
} catch (IOException e) {
e.printStackTrace();
}
while(isRunning){
try {
Sample sample = queue.take();
writer.write(sample.buffer, sample.bufferSize);
} catch (IOException e) {
//snip
}
}
这似乎做工精细,但最终的结果可辨别包含不管我说的,但它是可怕的扭曲。这里有大胆的屏幕顶盖(WMP拒绝播放该文件)。
Which appears to work fine, however the end result recognizably contains whatever I said, but it is horribly distorted. Here's a screen cap of audacity (WMP refuses to play the file).
任何帮助将是很大的AP preciated,让我知道如果你需要更多的code /信息。
Any help would be greatly appreciated, let me know if you need more code / info.
更新
随着马库斯的建议,这是我的更新code:
Following markus's suggestions, here is my updated code:
int sampleRate = 16000;
writer = new WaveWriter("/sdcard","recOut.wav",sampleRate,android.media.AudioFormat.CHANNEL_IN_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = AudioRecord.getMinBufferSize(sampleRate,android.media.AudioFormat.CHANNEL_IN_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT);
AudioRecord ar = new AudioRecord(AudioSource.MIC,sampleRate,android.media.AudioFormat.CHANNEL_IN_MONO,android.media.AudioFormat.ENCODING_PCM_16BIT,bufferSize);
byte[] buffer = new byte[bufferSize]; //all references changed from short to byte type
...而另一个大胆screencap:
...and another audacity screencap:
推荐答案
CHANNEL_CONFIGURATION_MONO是德precated据我所知,使用CHANNEL_IN_MONO。考虑寻找 rehearsalassistant 项目,AFAIK他们使用AudioRecord类也。 samplerates应选择符合标准的采样率就像这个维基百科的文章的解释。
CHANNEL_CONFIGURATION_MONO is deprecated as far as i know, use CHANNEL_IN_MONO. consider looking at rehearsalassistant project, afaik they use AudioRecord class, too. samplerates should be chosen to fit the standard sample rates like explained in this wikipedia article.
这篇关于录制WAV文件从Android的麦克风 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!