6 Replies
      Latest reply: Jan 12, 2017 2:04 PM by eskimo RSS
      Jiangsu HuaYu Intelligence Tec Level 1 Level 1 (0 points)

        I am developing  with  audioQueueService 

        But stop in function "CFURLCreateFromFileSystemRepresentation"

        here is my code but always get nil for return.


        var path = Bundle.main.path(forResource: "123", ofType: "mp3")!
        var xyz = UInt8(path.utf8CString[0])
        let audioFileURL =  CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,&xyz, path.characters.count, false)

        I had try post path's point to func but not work?

        can anyone help me?

        • Re: CFURLCreateFromFileSystemRepresentation
          OOPer Level 7 Level 7 (3,325 points)

          You'd better know that `URL` is convertible to `CFURL` with `as`-casting (it's safe!).

              let url = Bundle.main.url(forResource: "123", withExtension: "mp3")!
              let audioFileURL =  url as CFURL
              print(audioFileURL) //->file:///var/folders/q8/jkwxdz654tg2mx8h4lqg1zqw0000gt/T/com.apple.dt.Xcode.pg/resources/A62B1D61-8B36-4C4A-8B65-AFDB5A6494B2/123.mp3
              //The output (actual path) may be different in your environment.

          You have no need to use `CFURLCreateFromFileSystemRepresentation`.


          If you dare choose using `CFURLCreateFromFileSystemRepresentation`, you may need to write something like this:

          let path = Bundle.main.path(forResource: "123", ofType: "mp3")!
          let audioFileURL = path.withCString {xyz->CFURL in
              let bufLen = path.utf8.count //<- needs to be `path.utf8.count`, not `path.characters.count`
              return xyz.withMemoryRebound(to: UInt8.self, capacity: bufLen) {buffer in
                  CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, buffer, bufLen, false)