我设置了一堆音频节点
var audioPlayers = [AVAudioPlayerNode]()
var engine = AVAudioEngine()
var player = AVAudioPlayerNode()
@IBAction func key(_ sender: UIButton) {
audioPlayers[someindex].play()
}
func createPlayers() {
for key in constructedArrayOfNotes {
player = AVAudioPlayerNode()
player.volume = 0.5
let reverb = AVAudioUnitReverb()
reverb.loadFactoryPreset(AVAudioUnitReverbPreset.cathedral)
reverb.wetDryMix = 50
let audiopath = Bundle.main.path(forResource: key, ofType: "mp3",inDirectory: directory)!
let url = NSURL.fileURL(withPath: audiopath)
do {
let file = try! AVAudioFile.init(forReading: url)
let buffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length))
try file.read(into: buffer!)
engine.attach(player)
engine.attach(reverb)
engine.connect(player, to: reverb, format: buffer!.format)
engine.connect(reverb, to: engine.mainMixerNode, format: buffer!.format)
player.scheduleFile(file, at: nil, completionHandler: nil)
audioPlayers.append(player)
} catch {
print("error")
}
}
}
override func viewDidLoad() {
createPlayers()
engine.prepare()
try! engine.start()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
有没有办法让我从头开始重播存储在 AVAudioPlayerNode 中的音频文件?目前,当我按下 IBAction 时,声音会播放,但我无法重播。我正在尝试做相当于 AVAudioPlayer.starttime = 0