To use Audio Toolbox on iOS via Kotlin Native, I h...
# kotlin-native
a
To use Audio Toolbox on iOS via Kotlin Native, I have the following code: var url = NSBundle.mainBundle.URLForResource(file, "mp3") var id:Int = 0 val status = AudioServicesCreateSystemSoundID(url, cValuesOf(id)) AudioServicesPlaySystemSound(id) However, the url should be CFURLRef for AudioServices instead of NSURL returned by the URLForResource method. I tried bridging using: var cfurl = CFBridgingRetain(url) as CFURLRef val status = AudioServicesCreateSystemSoundID(cfurl, cValuesOf(id)) But, that throws a class cast. Not casting as CFURLRef results in a compiler error. Appreciate any pointers on what to try.
Looks like toll-free bridging on iOS is not supported in Kotlin Native?
s
Copy code
var cfurl = CFBridgingRetain(url) as CFURLRef
This should work. Maybe your
url
is
null
?
You also have another bug in this code, which isn’t related to the exception.
Copy code
var id:Int = 0
val status = AudioServicesCreateSystemSoundID(url, cValuesOf(id))
This wouldn’t work as expected, because you pass the value of
id
variable instead of the pointer. So this call wouldn’t update your variable. To get the id from
AudioServicesCreateSystemSoundID
, you can use something like
Copy code
val id = memScoped {
    val idVar = alloc<SystemSoundIDVar>()
    AudioServicesCreateSystemSoundID(cfurl, idVar.ptr)
    idVar.value
}