Get the path to the file in the dataset located in Assets.xcassets

I have a set of sound files in my Assets.xcassets:

Assets.xcassets

I am trying to get the path to one of these audio files, for example:

let path: String = Bundle.main.path(forResource: "acoustic_grand_piano/A4", ofType: "f32")!

But I get it EXC_BAD_INSTRUCTION. I tried looking on the Internet, but found nothing in the datasets.

How can I get the contents of one of these files?

Thank!

+4
source share
1 answer

Try the following:

  • Manually paste the files into a folder, naming anything.
  • Add ".bundle" to the package folder. You will get a warning, accept it. Congratulations, you just created your first kit! :-)
  • Manually drag this folder into your application.
  • , ....

    public func returnFile(_ named:String) -> String {
        let path: String = Bundle.main.path(forResource: "myAudioFiles", ofType: "bundle")! + "/" + name + ".f32"        
        do {
            return try String(contentsOfFile: path)
        }
        catch let error as NSError {
            return error.description
        }
    }
    

CIKernel. , , - .

EDIT:

, / . , :

public func returnFile(_ resource:String, _ fileName:String, _ fileType:String) -> String {
    let identifier = "com.companyname.appname" // replace with framework bundle identifier
    let fileBundle = Bundle.init(identifier: identifier)
    let filePath = (fileBundle?.path(forResource: resource, ofType: "bundle"))! + "/" + fileName + "." + fileType
do {
    return try String(contentsOfFile: filePath)
}
catch let error as NSError {
    return error.description
}

}

+2

Source: https://habr.com/ru/post/1663675/


All Articles