CFURLCreateFromFileSystemRepresentation

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)

print(audioFileURL)

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

can anyone help me?

Accepted Reply

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)
    }
}
print(audioFileURL)

Replies

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)
    }
}
print(audioFileURL)

If you dare choose using

CFURLCreateFromFileSystemRepresentation
, you may need to write something like this:

That’s way more complex than necessary; you can just rely on Swift’s

String
to C string support.
let url = CFURLCreateFromFileSystemRepresentation(nil, p, p.utf8.count, false)!

Alternatively, use

CFURLCreateWithFileSystemPath
.
let url = CFURLCreateWithFileSystemPath(nil, p as NSString, .cfurlposixPathStyle, false)!

However, I agree with your main point: this stuff is much easier if you work in ‘URL’ space, relying on Swift ability to easily convert

URL
into both NSURL and CFURL.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, eskimo. Maybe I was too urgent to tell the first part.

Thank you very much!

hi eskimo

thank you for your replay!

I am learnning AudioQueuServicesGuide and write code with swift.

But too few infomation i can get, It's full of thorns when i wrote code.

I was confused with this,so can you lead me a way to handle with this?


if possible can we communicate with email?

zj692147092@icould.com

if possible can we communicate with email?

Well, my email address is in my signature, so it’s easy to get in touch. However, I not able to provide technical support via email. If you post questions to DevForums, I'll try to help out here. Otherwise, for formal support, you can open a DTS tech support incident.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"