运行 ARKit 2.0 with a ARSCNView. iOS12该应用程序使用多线程,这就是在主线程上执行这些功能的原因(只是为了确保)。我也尝试没有在主线程上显式执行功能,但无济于事。
我正在使用 .aiff 声音文件,但也尝试了.wav。没有快乐。
我什至尝试从节点层次结构中删除audioNode_alarm,声音仍然播放。我什至从视图层次结构中删除了 ARSCNView,声音仍然播放。农民田间学校
从我所看到的,我正在完全按照我应该做的事情,以阻止音频播放。无论我尝试什么,音频都不会停止。谁能想到为什么?!
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()
}
}
我最终做的是停止声音并删除播放器
yourNode.audioPlayers.forEach { audioLocalPlayer in
audioLocalPlayer.audioNode?.engine?.stop()
yourNode.removeAudioPlayer(audioLocalPlayer)
}
根据文档SCNAudioPlayer具有audioNode,它应该用于"在播放过程中实时改变音量和混响等参数"。
audioNode 是 AVAudioNode 类型,所以如果我们跳到引擎 prop 及其类型定义,我们会找到我们需要的所有控件。