CFURLRef requires a bridged cast error

2020-04-07 03:35发布

I'm trying to get a sound to play but when I code:

AudioServicesCreateSystemSoundID ((CFURLRef) alertSound, &soundFileObject);

it throws the following error:

Cast of Objective-C pointer type 'NSURL *' to C pointer type 'CFURLRef' (aka 'const struct __CFURL *') requires a bridged cast error

I have tried both of the two following suggested solutions:

AudioServicesCreateSystemSoundID ((__bridge CFURLRef) alertSound, &soundFileObject);

or

AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(alertSound), &soundFileObject);

But I still can't get the sound to play.

I guess the question is: is the bridging error the cause of the sound not playing, or should I be looking else where?

I can get the sound to play using the SysSound sample code and I'm using iOS 6 and Xcode 4.5.

Thanks for any pointers :)

1条回答
地球回转人心会变
2楼-- · 2020-04-07 04:19

The cast is probably not the reason the sound is not playing:

AudioServicesCreateSystemSoundID ((__bridge CFURLRef) alertSound, &soundFileObject);

is fine.

Most likely your error is in code that you have not shown us. A common problem is an error in the URL creation or an invalid file format.

Modify your code as follows:

OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef)alertSound, &soundFileObject);
NSLog(@"AudioServicesCreateSystemSoundID status = %ld", status);

and check what status code you get; parmErr (-50) is relatively common and usually means your URL is nil.

Oh, and one more thought, you don't show where you're actually playing the sound. You do know that AudioServicesCreateSystemSoundID does not actually play the sound. You need to call:

AudioServicesPlaySystemSound(soundFileObject);

Apologies if you knew that already!

查看更多
登录 后发表回答