Using AVAudioPlayerNode, you can schedule multiple buffers and files, and they will (or at least should) play in sequence. However, I'm finding that some sequences don't work. If you schedule a file after two or more buffers, the file doesn't play. Other combinations work. For example, scheduling a file after only one buffer works, as does scheduling buffers after files, and so on.
Here's a self-contained example (except for the audio file) that demonstrates the behavior (this can be pasted over the contents of the 'ViewController.swift' file in the Xcode iOS 'App' template):
It seems like this should play the sound three times, but it only plays it twice.
Is this a bug? Or is there something about the API I'm not understanding?
Here's a self-contained example (except for the audio file) that demonstrates the behavior (this can be pasted over the contents of the 'ViewController.swift' file in the Xcode iOS 'App' template):
Code Block import AVFoundation import UIKit class ViewController: UIViewController { private let engine = AVAudioEngine() private let player = AVAudioPlayerNode() private var file: AVAudioFile! = nil private var buffer: AVAudioPCMBuffer! = nil override func viewDidLoad() { super.viewDidLoad() let url = Bundle.main.resourceURL!.appendingPathComponent("audio.m4a") file = try! AVAudioFile(forReading: url) buffer = AVAudioPCMBuffer( pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length) ) try! file.read(into: buffer) engine.attach(player) engine.connect( player, to: engine.mainMixerNode, format: file.processingFormat ) try! engine.start() player.play() player.scheduleBuffer(buffer) player.scheduleBuffer(buffer) player.scheduleFile(file, at: nil) } }
It seems like this should play the sound three times, but it only plays it twice.
Is this a bug? Or is there something about the API I'm not understanding?