2

I am trying the Qt project that captures audio data from mic on android. I refereed this article : Android AudioRecord example, and wrote it to Qt code.

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);
}

But, at this line int result = mAudioRecorder->callMethod("read", "([BII)I", buffers, offset, size); the app crashed and outputted following log.

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)

I guessed this error was cause of bad JNI calling, but I couldn't find the reason.

Addition: I found 0x61382e48 is a address of buffers array

EDIT : Android guide for AudioRecord is here.

Welcome any advices.

Community
  • 1
  • 1
covernal
  • 329
  • 1
  • 3
  • 20
  • 1) Why did you comment out the `jByteArray` line? 2) Note that you're usng a non-standard C++ syntax for dynamic arrays here: `jint size = BufferElements2Rec * BytesPerElement;jbyte buffers[size * 2];` Don't know if it affects the function call though, but something to consider. – PaulMcKenzie Feb 12 '15 at 19:36
  • @PaulMcKenzie Ignore the `jByteArray` line. That is not the code of this function. – covernal Feb 13 '15 at 03:33

0 Answers0