Yan Pujante
03/16/2024, 6:31 PMYan Pujante
03/16/2024, 6:34 PMsuspend fun loadAudio(url: String): Audio {
val audio = Audio(url)
suspendCoroutine { continuation ->
var listener: ((Event) -> Unit)? = null
listener = { _: Event ->
println("listener = $listener")
if(listener != null) {
// This remove call does not seem to work... hence the listener = null line
audio.removeEventListener("canplaythrough", callback = listener)
listener = null
continuation.resume(Unit)
}
}
println("listener = $listener")
audio.addEventListener("canplaythrough", callback = listener)
}
return audio
}
The problem I am facing is that removeListener
does not work and the listener keeps on being called. I hacked it around by setting listener = null
otherwise continuation.resume(Unit)
fails when called again.
The same code in javascript works fine:
const sound = new Audio('Ping.wav');
const listener = (e) => {
sound.removeEventListener("canplaythrough", listener);
console.log('canplaythrough (listener)\n');
}
sound.addEventListener("canplaythrough", listener);
The listener is only called once and properly removed...Yan Pujante
03/17/2024, 5:30 PM