我正在使用 AVAudioEngine
播放声音文件并将输出记录到文件中。我有很多音效文件,每一个都通过点击一个按钮来播放。为了播放文件,我创建了一个 AVAudioPlayerNode
并将其连接到引擎。文件播放后,我尝试断开/分离 completionHandler 闭包中的节点,以释放内存。如果我不删除节点,我将继续向引擎添加新节点和新连接。
但是,下次我尝试播放文件时,应用程序就死机了。这是我的代码:
let url = NSBundle.mainBundle().URLForResource(name, withExtension: "wav")!
let audioPlayerFile = try AVAudioFile(forReading: url)
let playerNode = AVAudioPlayerNode()
self.engine.attachNode(playerNode)
self.engine.connect(playerNode, to: self.engine.mainMixerNode, format: audioPlayerFile.processingFormat)
playerNode.scheduleFile(audioPlayerFile, atTime: nil, completionHandler: { () -> Void in
self.engine.disconnectNodeOutput(playerNode)
self.engine.detachNode(playerNode)
})
playerNode.play()
实现此类功能的最佳方式是什么,即在录制时播放多个文件,可能彼此重叠?
最佳答案
我遇到过类似的问题。我发现在后台线程上分离节点可以防止应用程序卡住。
playerNode.scheduleBuffer(buffer, atTime: nil, options: nil) { () -> Void in
self.detach(playerNode)
}
方法如下:
func detach(player: AVAudioPlayerNode) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) { () -> Void in
self.engine.detachNode(player)
}
但是,我仍然不确定这本身就是一个解决方案,因为这些后台线程可能在我不知情的情况下无限旋转。
关于ios - 使用后删除 AVAudioPlayerNode 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34088916/