在iOS 10上运行我可以根据需要将文件中的样本加载到AVAudioUnitSampler中,并且可以正常播放。 但是,当我在iOS 11上运行相同的代码时,音频无法播放。为了使它工作,我必须预加载音频。
iOS 11中有什么变化导致它不再起作用吗?
设计用于从大约100个样本中选择,其中一些样本比其他样本长,因此我无法提前将它们全部加载到内存中,因此需要按需加载。
我已经在iOS 11,11.0.1和11.0.2上试过了。
有什么建议吗?
这是我的初始版本(适用于iOS 10但不适用于iOS 11):
var audioEngine:AVAudioEngine!
var mixer:AVAudioMixerNode!
var sampler:AVAudioUnitSampler!
override func viewDidLoad() {
super.viewDidLoad()
audioEngine = AVAudioEngine()
mixer = audioEngine.mainMixerNode
mixer.volume = 1.0
do {
try audioEngine.start()
} catch {
print(error)
}
sampler = AVAudioUnitSampler()
audioEngine.attach(sampler)
audioEngine.connect(sampler, to: mixer, format: nil)
}
@IBAction func audio1Tapped(_ sender: Any) {
if let audioPath = Bundle.main.url(forResource: "a73", withExtension: "wav") {
do {
try sampler.loadAudioFiles(at: [audioPath])
sampler.startNote(60, withVelocity: 127, onChannel: 0)
} catch {
print(error.localizedDescription)
}
} else {
print("Failed to find audio file")
}
}
以下适用于iOS 11:
var audioEngine:AVAudioEngine!
var mixer:AVAudioMixerNode!
var sampler:AVAudioUnitSampler!
override func viewDidLoad() {
super.viewDidLoad()
audioEngine = AVAudioEngine()
mixer = audioEngine.mainMixerNode
mixer.volume = 1.0
do {
try audioEngine.start()
} catch {
print(error)
}
sampler = AVAudioUnitSampler()
audioEngine.attach(sampler)
audioEngine.connect(sampler, to: mixer, format: nil)
if let audioPath = Bundle.main.url(forResource: "a73", withExtension: "wav") {
do {
try sampler.loadAudioFiles(at: [audioPath])
} catch {
print(error.localizedDescription)
}
} else {
print("Failed to find audio file")
}
}
@IBAction func audio1Tapped(_ sender: Any) {
sampler.startNote(60, withVelocity: 127, onChannel: 0)
}