Search code examples
swiftaudiokit

Trouble with Audiokit's AKAudioFile base directory input


I am simply trying to play a single audio file. I am trying to set it up in the same way the audiokit playgrounds did, but I seem to be missing how to specify an AKAudiofile's base directory. I need to know how to properly link a file to my code.

I have tried doing exactly what the playground specifies, but apperently within a playground, the base directory is defaulted, and I can't seem to find a way to specify it in my code.

let file = try AKAudioFile(readFileName: "sound.wav", baseDir:    AKAudioFile.BaseDirectory.resources) { 
print("Failed")}

I'm unsure of what is wrong, within my code I have a folder titled resources, but if its a problem with the folder not being found I would assume it crashes on build rather than it giving me a beforehand error.

This gives me error: "Argument labels '(readFileName:, baseDir:, _:)' do not match any available overloads"

I have no clue what this means


Solution

  • Your compiler is right, what you've written does not match anything in AudioKit. What you have in brackets is interpreted as a trailing closure, and the last parameter _: in the method signature. Instead, you should be using a do try catch block like this:

    var file: AKAudioFile
    
    do {
       file = try AKAudioFile(readFileName: "sound.wav", baseDir: .resources) 
    } catch { 
       print("Failed")
    }
    

    Also be sure you're using the actual resources folder Xcode creates, not just a file you create called Resources. And make sure "sound.wav" is included in the build (from the inspector menu).