androidc++kotlinandroid-ndkoboe

Main UI Thread is blocked once oboe recording is started


I have coded a basic application that starts recording with oboe library. In MainActivity there are 2 buttons that call 2 JNI functions that are :

In native-lib.cpp, these 2 JNI functions are defined as follows :

extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_oboeaudiorecorder_MainActivity_recordAudio(
        JNIEnv * env,
        jobject MainActivity
) {
    static auto a = OboeAudioRecorder::get();
    a->StartAudioRecorder();
    return true;
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_oboeaudiorecorder_MainActivity_stopRecording(
        JNIEnv * env,
        jobject MainActivity
        ) {
    static auto a = OboeAudioRecorder::get();
    a->StopAudioRecorder();
    return true;
}

The OboeAudioRecorder class is a singleton.

When I click on the button for starting the recording, the recording is well started. But then when I want to click on the button for stopping the recording, the button cannot be clicked. I think that the start of oboe recording is blocking the main UI thread.

The OboeAudioRecorder singleton class can be viewed at : https://github.com/reuniware/OboeAudioRecorder/blob/master/app/src/main/cpp/OboeAudioRecorder.cpp

How to avoid that ? Thanks.


Solution

  • I now call the recordAudio function from a thread as follows :

    buttonRecordAudio.setOnClickListener{
        val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
    
        if (permission != PackageManager.PERMISSION_GRANTED) {
            Log.i("", "Permission to record denied")
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_REQUEST_CODE)
        } else {
            Thread(Runnable { recordAudio() }).start()
        }
    }