使用 ARSCNView 运行 ARKit 2.0。iOS12 应用程序使用多线程,这就是为什么这些功能在主线程上执行(只是为了确定)。我也尝试过没有在主线程上显式执行功能,但无济于事。
我正在使用 .aiff 声音文件,但也尝试过 .wav。没有喜悦。
我什至尝试从节点层次结构中删除 audioNode_alarm 并且声音仍在播放。我什至从视图层次结构中删除了 ARSCNView 并且声音仍然播放。FFS
据我所知,我正在按照我应该做的事情做,以阻止音频播放。无论我尝试什么,音频都不会停止。谁能想到为什么?!
weak var audioNode_alarm: SCNNode!
weak var audioPlayer_alarm: SCNAudioPlayer?
func setupAudioNode() {
let audioNode_alarm = SCNNode()
addChildNode(audioNode_alarm)
self.audioNode_alarm = audioNode_alarm
}
func playAlarm() {
DispatchQueue.main.async { [unowned self] in
self.audioNode_alarm.removeAllAudioPlayers()
if let audioSource_alarm = SCNAudioSource(fileNamed: "PATH_TO_MY_ALARM_SOUND.aiff") {
audioSource_alarm.loops = true
audioSource_alarm.load()
audioSource_alarm.isPositional = true
let audioPlayer_alarm = SCNAudioPlayer(source: audioSource_alarm)
self.audioNode_alarm.addAudioPlayer(audioPlayer_alarm)
self.audioPlayer_alarm = audioPlayer_alarm
}
}
}
func stopAlarm() {
DispatchQueue.main.async { [unowned self] in
self.audioNode_alarm?.removeAudioPlayer(self.audioPlayer_alarm!)
self.audioNode_alarm?.removeAllAudioPlayers()
}
}