HomeiOS Developmentandroid - Kotlin Mutiplarform throws NPE with AVAudioRecorder

android – Kotlin Mutiplarform throws NPE with AVAudioRecorder


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

memScoped {
    attempt {
        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 (/choose/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/major/kotlin/kotlin/Exceptions.kt:32:28)
at 3   shared                              0x10b7609b9        kfun:kotlin.NullPointerException#<init>(){} + 73 (/choose/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/major/kotlin/kotlin/Exceptions.kt:43:28)
at 4   shared                              0x10b7862d4        ThrowNullPointerException + 132 (/choose/buildAgent/work/acafc8c59a79cc1/kotlin/kotlin-native/runtime/src/major/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 drawback calling this:

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

This works as anticipated and I can document 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

Most Popular

Recent Comments