0

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 :

  • recordAudio()
  • stopRecording()

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.

KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • 1
    looks like `StartAudioRecorder` is blocking, just call `recordAudio` from a thread? – Alan Birtles Jul 03 '20 at 20:14
  • Yes that works. I did not think of creating the thread from the kotlin code, I had tried to start the thread from JNI with std::thread but I had a SIGABRT error... But as you told me I have done it from kotlin with Thread(Runnable{recordAudio}).start() and now everything works fine ! Thank you (sometimes the simplest is the more efficient). – KotlinIsland Jul 03 '20 at 20:30

1 Answers1

0

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()
    }
}
KotlinIsland
  • 799
  • 1
  • 6
  • 25