AVAudioUnit加载预设失败

时间:2019-02-27 02:38:08

标签: swift avaudioplayer avaudioengine

我使用Apple的AULab软件创建了一个简单的.aupreset,它是一个混响预设(我想将其添加到音频文件中,并使音频文件具有该混响水平)。当尝试将预设加载到AVAudioUnit中时 总是出现此错误:

  

Error Domain = Foundation._GenericObjCError Code = 0“(null)”

如果我使用try!这是崩溃的消息:

  

线程1:致命错误:“尝试!”表达式意外引发错误:Foundation._GenericObjCError.nilError

导致错误的代码行:

try customPresetUnit.loadPreset(at: presetURL)

这是整个方法:

let player = AVAudioPlayerNode()
let engine = AVAudioEngine()
let reverb = AVAudioUnitReverb()
let customPresetUnit = AVAudioUnit()

func addPresetToIntro(){
    guard let introductionAudio = Bundle.main.url(forResource: "Introduction", withExtension: "m4a") else{fatalError("Introduction file not found")}
    guard let presetURL = Bundle.main.url(forResource: "FirstPreset", withExtension: "aupreset") else{fatalError("preset not found")}
    do {
        try customPresetUnit.loadPreset(at: presetURL)
    }
    catch {
        print((error as NSError))
        return
    }

    let audioFile = try! AVAudioFile(forReading: introductionAudio)
    engine.attach(player)
    engine.attach(customPresetUnit)
    engine.connect(player, to: customPresetUnit, format: nil)
    engine.connect(customPresetUnit, to: engine.mainMixerNode, format: nil)

    player.scheduleFile(audioFile, at: nil, completionHandler: nil)
    try! engine.start()
    player.play()
}

任何想法都值得赞赏!

注意:是的,我知道我可以使用AVAudioUnitReverb创建混响效果,但是当我创建更复杂的文件时,我想使其与.aupreset文件一起使用。

0 个答案:

没有答案