Android audiorecord 实例失败
我正在创建一个应用程序来检测通过麦克风播放的声音的频率。 分析是在 AsyncTask 中完成的。当我尝试一个音符时它就起作用了。
然而,当我尝试分析 6 个音符时,例如来自不同吉他弦的 6 个音符,一个接着一个(中间有一秒左右的延迟),它可能只捕获了另一个音符。
这是我得到的一些 LogCat 输出:
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=-1
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [SKW]do_route_audio_rpc(0, 1, 0)
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): msm72xx_enable_audpp: 0x0000
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=0
11-22 19:50:30.523: ERROR/AudioHardwareMSM72XX(1296): Cannot open /dev/msm_pcm_in errno: 16
11-22 19:50:30.533: ERROR/AudioRecord(23241): Could not get audio input for record source 1
11-22 19:50:30.533: ERROR/AudioRecord-JNI(23241): Error creating AudioRecord instance: initialization check failed.
11-22 19:50:30.533: ERROR/AudioRecord-Java(23241): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
我为大家删除了所有重复内容。
这是我的课的要点:
static AudioRecord recorder;
static short[] audioData;
static int bufferSize;
static int samplerate = 8000;
static boolean recorded = false;
@Override
protected Integer doInBackground(params) {
bufferSize= AudioRecord.getMinBufferSize(samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT)*2;
recorder = new AudioRecord (AudioSource.MIC,samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize);
audioData = new short [bufferSize]; //short array that pcm data is put into.
int recordingLoops = 0;
while (recordingLoops < 4 || recorded) { //loop until recording is running
if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED)
if(recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
recorder.startRecording();
else {
recorder.read(audioData,0,bufferSize);
getFrequencyFromAudiodata(audioData);
recorded = true;
}
recordingLoops++;
}
if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING)
{
killRecorder();
}
return 1;
}
你们知道发生了什么吗?我环顾四周,但似乎只是遇到了一些人说 Android“充满了错误”。
任何帮助将不胜感激。
谢谢,本
I am creating an app that detects frequencies of sound playing through the mic.
The analysis is done in an AsyncTask. It works when I try one note.
However, when I try to analyse 6 notes, say from different guitar strings, one after the other (with a delay of a second or so in between) it only captures maybe one more.
This is some of the LogCat output I get:
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=-1
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [SKW]do_route_audio_rpc(0, 1, 0)
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): msm72xx_enable_audpp: 0x0000
11-22 19:50:04.333: ERROR/AudioHardwareMSM72XX(1296): [doRouting]sndDevice=0,mCurSndDevice=0
11-22 19:50:30.523: ERROR/AudioHardwareMSM72XX(1296): Cannot open /dev/msm_pcm_in errno: 16
11-22 19:50:30.533: ERROR/AudioRecord(23241): Could not get audio input for record source 1
11-22 19:50:30.533: ERROR/AudioRecord-JNI(23241): Error creating AudioRecord instance: initialization check failed.
11-22 19:50:30.533: ERROR/AudioRecord-Java(23241): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
I deleted any repetitions for you all.
Here is the jist of my class:
static AudioRecord recorder;
static short[] audioData;
static int bufferSize;
static int samplerate = 8000;
static boolean recorded = false;
@Override
protected Integer doInBackground(params) {
bufferSize= AudioRecord.getMinBufferSize(samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT)*2;
recorder = new AudioRecord (AudioSource.MIC,samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize);
audioData = new short [bufferSize]; //short array that pcm data is put into.
int recordingLoops = 0;
while (recordingLoops < 4 || recorded) { //loop until recording is running
if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED)
if(recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
recorder.startRecording();
else {
recorder.read(audioData,0,bufferSize);
getFrequencyFromAudiodata(audioData);
recorded = true;
}
recordingLoops++;
}
if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING)
{
killRecorder();
}
return 1;
}
Have you guys got any idea what is happening? I have done some looking around but I seem to just be running into people saying Android is 'full of bugs'.
Any help would be massively appreciated.
Thanks, Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案在于:
我将其更改为:
并且仅当音频数据数组足够填充时才将记录器设置为 true。
这意味着它会循环直到完美。感谢您的帮助戴夫!
The answer lay in this:
I changed it to:
And only set recorder to true when the array of audio data was sufficiently populated.
This meant it would loop until perfect. thanks for your help Dave!
您可能会看看这个答案,它看起来像是处理与 AudioRecord 相关的线程的一些想法:
Android AudioRecord类-快速处理现场麦克风音频,设置回调函数
You might look at this answer, it looks like some thought went into handling the threading associated with AudioRecord:
Android AudioRecord class - process live mic audio quickly, set up callback function