2

我正在尝试从 android 上的麦克风捕获音频数据的 Qt 项目。我参考了这篇文章:Android AudioRecord 示例,并将其写入 Qt 代码。

int recorderSampleRate = 44100;
int recorderChannels = QAndroidJniObject::getStaticField<jint>("android/media/AudioFormat", "CHANNEL_IN_MONO");
int recorderAudioEncoding =  QAndroidJniObject::getStaticField<jint>("android/media/AudioFormat", "ENCODING_PCM_16BIT");
int sourceType = QAndroidJniObject::getStaticField<jint>("android/media/MediaRecorder$AudioSource", "MIC");


int bsize = QAndroidJniObject::callStaticMethod<int>("android/media/AudioRecord", "getMinBufferSize", "(III)I", recorderSampleRate, recorderChannels, recorderAudioEncoding);

mAudioRecorder = new QAndroidJniObject("android/media/AudioRecord", "(IIIII)V", sourceType, recorderSampleRate, recorderChannels, recorderAudioEncoding, BufferElements2Rec * BytesPerElement);


// starting capture.
mAudioRecorder->callMethod<void>("startRecording", "()V");

// ready buffer.
jint size = BufferElements2Rec * BytesPerElement;
jbyte buffers[size * 2];

qDebug() << "start capture";

while(true)
{
    {
        jint offset = 0;

        // read cached audio data from AudioRecord
        int result = mAudioRecorder->callMethod<jint>("read", "([BII)I", buffers, offset, size);

        qDebug() << "readed byte size " << result;
    }

    // wait a bit for new audio data.
    msleep(10);
}

但是,在这一行 int result = mAudioRecorder->callMethod("read", "([BII)I", buffers, offset, size); 该应用程序崩溃并输出以下日志。

W/dalvikvm(24772): Invalid indirect reference 0x61382e48 in decodeIndirectRef
E/dalvikvm(24772): VM aborting
F/libc    (24772): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 24975 (ldAndroidCaptur)

我猜这个错误是 JNI 调用错误的原因,但我找不到原因。

补充: 我发现0x61382e48缓冲区数组的地址

编辑: AudioRecord 的 Android 指南在这里

欢迎任何建议。

4

0 回答 0