我正在尝试使用
我希望使用一个
看起来播放文件的方法是
是否可能使用
AVAudioPlayerNode
播放来自Assets.xcassets
资源目录的声音,但我无法弄清楚如何实现。
我一直在使用AVAudioPlayer
,可以使用NSDataAsset
进行初始化,如下所示:
let sound = NSDataAsset(name: "beep")!
do {
let player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeWAVE)
player.prepareToPlay()
player.play()
} catch {
print("Failed to create AVAudioPlayer")
}
我希望使用一个
AVAudioPlayerNode
(用于调音等原因)。我可以创建引擎并连接节点:var engine = AVAudioEngine()
func playSound(named name: String) {
let mixer = engine.mainMixerNode
let playerNode = AVAudioPlayerNode()
engine.attach(playerNode)
engine.connect(playerNode, to: mixer, format: mixer.outputFormat(forBus: 0))
// play the file (this is what I don't know how to do)
}
看起来播放文件的方法是
playerNode.scheduleFile()
。它需要一个AVAudioFile
,所以我想尝试制作一个。但是AVAudioFile
的初始化器需要一个URL。据我所知,资源目录中的资源不可通过URL获得。我可以使用NSDataAsset
直接获取数据,但似乎没有任何方法可以将其用于填充AVAudioFile
。是否可能使用
AVAudioPlayerNode
从资产目录播放声音?如果可以,该怎么做?