带有定位音频 SCNAudioPlayer 的 ARKit SceneKit ARSCNView 不会停止播放
ARKit SceneKit ARSCNView with Positional Audio SCNAudioPlayer WILL NOT STOP playing
运行 带有 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()
}
}
我最后做的是通过
停止声音并移除播放器
yourNode.audioPlayers.forEach { audioLocalPlayer in
audioLocalPlayer.audioNode?.engine?.stop()
yourNode.removeAudioPlayer(audioLocalPlayer)
}
根据文档 SCNAudioPlayer has audioNode,应该使用 "to vary parameters such as volume and reverb in real time during playback"。
audioNode是AVAudioNode类型,所以如果我们跳转到engine prop and its type定义,我们会找到所有的控件我们需要。
运行 带有 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()
}
}
我最后做的是通过
停止声音并移除播放器yourNode.audioPlayers.forEach { audioLocalPlayer in
audioLocalPlayer.audioNode?.engine?.stop()
yourNode.removeAudioPlayer(audioLocalPlayer)
}
根据文档 SCNAudioPlayer has audioNode,应该使用 "to vary parameters such as volume and reverb in real time during playback"。
audioNode是AVAudioNode类型,所以如果我们跳转到engine prop and its type定义,我们会找到所有的控件我们需要。