loadAudioFiles抛出错误

时间:2017-04-01 18:39:46

标签: swift avfoundation

我正在使用以下代码段:

import AVFoundation


var ae:AVAudioEngine
var sampler:AVAudioUnitSampler
var mixer:AVAudioMixerNode

var midiNoteNumberFor:Dictionary<String,UInt8> = [
    "BD":48,
    "Snr":50,
    "Hat":52,
    "Hit":53,
    "VI":68,
    "V":67,
    "i":60,
    "III": 63
]
    let urls = Bundle.main.urls(forResourcesWithExtension: "wav", subdirectory: "wavs")

    ae = AVAudioEngine()
    mixer = ae.mainMixerNode
    sampler = AVAudioUnitSampler()

    ae.attach(sampler)
    ae.connect(sampler, to: mixer, format: sampler.outputFormat(forBus: 0))

    do {
        try sampler.loadAudioFiles(at: urls!)

    } catch {
        print(error)
    }


    do {
        try ae.start()

    } catch {
        print("Error info: \(error)")
    }


func play(snd:String){
    sampler.startNote(midiNoteNumberFor[snd]!, withVelocity: 127,onChannel: 0)

}

但是,当我运行函数play(snd: "BD")时,它会抛出以下错误:

  

错误Domain = com.apple.coreaudio.avfaudio Code = -10851&#34;(null)&#34;   UserInfo = {failed call = error}

来自第一个catch子句。

1 个答案:

答案 0 :(得分:0)

在构建设置中,您需要向wavs目录添加文件引用。