Main UI 双簧管录音开始后线程被阻塞
Main UI Thread is blocked once oboe recording is started
我编写了一个基本应用程序,它开始使用双簧管库进行录音。
在 MainActivity 中有 2 个按钮调用 2 个 JNI 函数:
- recordAudio()
- 停止录音()
在native-lib.cpp中,这2个JNI函数定义如下:
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;
}
OboeAudioRecorder class 是单例。
当我点击开始录音的按钮时,录音已经开始了。
但是当我想点击停止录制的按钮时,按钮无法点击。
我认为双簧管录音的开始阻塞了主线程 UI。
OboeAudioRecorder 单例 class 可以在以下位置查看:
https://github.com/reuniware/OboeAudioRecorder/blob/master/app/src/main/cpp/OboeAudioRecorder.cpp
如何避免这种情况?谢谢
我现在从线程中调用 recordAudio 函数,如下所示:
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()
}
}
我编写了一个基本应用程序,它开始使用双簧管库进行录音。 在 MainActivity 中有 2 个按钮调用 2 个 JNI 函数:
- recordAudio()
- 停止录音()
在native-lib.cpp中,这2个JNI函数定义如下:
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;
}
OboeAudioRecorder class 是单例。
当我点击开始录音的按钮时,录音已经开始了。 但是当我想点击停止录制的按钮时,按钮无法点击。 我认为双簧管录音的开始阻塞了主线程 UI。
OboeAudioRecorder 单例 class 可以在以下位置查看: https://github.com/reuniware/OboeAudioRecorder/blob/master/app/src/main/cpp/OboeAudioRecorder.cpp
如何避免这种情况?谢谢
我现在从线程中调用 recordAudio 函数,如下所示:
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()
}
}