android – Kotlin Mutiplarform throws NPE with AVAudioRecorder


I am testing Kotlin Multiplatform to create an app PoC to file audio from Android/iOS. I’ve a standard interface to encapsulate the native logic and for iOS I am writting this code to create the recorder:

memScoped {
    strive {
        val error: ObjCObjectVar<NSError?> = alloc()
        recorder = AVAudioRecorder(url, settings, error.ptr)
        recorder?.meteringEnabled = true
        recorder?.prepareToRecord()
        println(error)
    } catch (e: Exception) {
        println(e.toString())
        e.printStackTrace()
    }
}

The issue is that I at all times get a NPE within the name to the constructor for AVAudioRecorder that’s a part of the AVFoundation of iOS.

at 2   shared                              0x10b760779        kfun:kotlin.RuntimeException#<init>(){} + 73 (/decide/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/predominant/kotlin/kotlin/Exceptions.kt:32:28)
at 3   shared                              0x10b7609b9        kfun:kotlin.NullPointerException#<init>(){} + 73 (/decide/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/predominant/kotlin/kotlin/Exceptions.kt:43:28)
at 4   shared                              0x10b7862d4        ThrowNullPointerException + 132 (/decide/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/predominant/kotlin/kotlin/native/inner/RuntimeUtils.kt:15:11)
at 5   shared                              0x10b6d3aae        kfun:ar.com.p39.powernotes.recorder.IOSAudioRecordThread#startRecording(){} + 2910 

But when I wrote the iOS native half in swift and move the occasion from iOS code to kotlin, I’ve no downside calling this:

let settings: [String: Any] = [
    AVFormatIDKey: kAudioFormatLinearPCM,
    AVSampleRateKey: 44100.0,
    AVNumberOfChannelsKey: 2,
    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
var url = URL(string: outputFile)
do {
    recorder = strive AVAudioRecorder(url: url!, settings: settings)
    recorder?.isMeteringEnabled = true
    recorder?.prepareToRecord()
    recorder?.file()
catch {
    debugPrint("ERror")
}

This works as anticipated and I can file audio.

Any concepts easy methods to debug why the kotlin bridge to Obj-c crash? Thanks


Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles