PDA

View Full Version : This drives me nuts - CFBundleCopyResourceURL returns NULL




AZAstro
Sep 22, 2012, 02:34 PM
It's gotta be a bug I guess.

The following code in a very simple app. The top one works fine and the mirror below it doesn't..

// This method works fine.
- (IBAction)playHorn:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Horn", CFSTR ("aiff"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}

// This doesn't work for some reason because the function CFBundleCopyResourceURL line passes NULL back.
- (IBAction)play60m4a:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"60", CFSTR ("m4a"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}

****

20348#($*(*@_)(*)(*

Suggestions welcome. I am NOT an ace at objective C programming so when these things fail like this it drives me nuts.

The two files, 60.m4a and Horn.aiff both play fine in iTunes and when selected in the xCode navigator.



nickculbertson
Sep 22, 2012, 05:14 PM
The quick fix would be to convert your @"60" file to an aiff file in iTunes. I think I read that in the new simulator it doesn't play mp3s (could be true for other file types).

Nick

AZAstro
Sep 22, 2012, 06:27 PM
The quick fix would be to convert your @"60" file to an aiff file in iTunes. I think I read that in the new simulator it doesn't play mp3s (could be true for other file types).

Nick

I tried that and - no joy. And another thing - I've had some aif files NOT PLAY and others play fine, which suggests it may have something to do with the files themselves.

PhoneyDeveloper
Sep 23, 2012, 11:57 AM
I recommend that you stay away from CF functions as much as possible. Do you know what Copy means in that function name? Use objective-C to build your NSURL and NSStrings and use typecasts when needed to satisfy the compiler.

Anyway, the code you show looks simple enough and correct. Either the sounds are present in your app bundle, or not. And either the sounds are in the right format to be played, or not. If you get NULL when trying to get the resource url then you know what that means. If the resource URL is valid but the sound doesn't play then you know what that means.

Unfortunately some sounds play on the device and not the Sim, and the reverse.

Some notes from my code:

// The most compatible sound file format for iPhone is caf.
// Microsoft WAV PCM 16 bit also works.
// It's possible to convert to caf with:
// afconvert -f caff -d LEI16@44100 MYSOUND.WAV
// this will make it single channel:
// afconvert -f caff -d LEI16@44100 -c 1 MYSOUND.WAV

AZAstro
Sep 26, 2012, 11:05 PM
I recommend that you stay away from CF functions as much as possible. Do you know what Copy means in that function name? Use objective-C to build your NSURL and NSStrings and use typecasts when needed to satisfy the compiler.

Anyway, the code you show looks simple enough and correct. Either the sounds are present in your app bundle, or not. And either the sounds are in the right format to be played, or not. If you get NULL when trying to get the resource url then you know what that means. If the resource URL is valid but the sound doesn't play then you know what that means.

Unfortunately some sounds play on the device and not the Sim, and the reverse.

Some notes from my code:

// The most compatible sound file format for iPhone is caf.
// Microsoft WAV PCM 16 bit also works.
// It's possible to convert to caf with:
// afconvert -f caff -d LEI16@44100 MYSOUND.WAV
// this will make it single channel:
// afconvert -f caff -d LEI16@44100 -c 1 MYSOUND.WAV

Thank you very much for your comments.

I am getting a null back from the call to the bundle so I guess I'll have to address that first. I have been doing some internet-wide searches and not finding much on this issue.

Trying to put some final touches on an app but the playing of the sounds has been problematic...

PhoneyDeveloper
Sep 27, 2012, 07:21 AM
If you're really getting back NULL then that's a simple problem. I recommend you start by deleting your app from the Sim and any devices. Option-clean the build folder. Then rebuild.

If the problem persists then what does the Build phases say for these files? Are they really in your target?

AZAstro
Sep 29, 2012, 12:35 AM
If you're really getting back NULL then that's a simple problem. I recommend you start by deleting your app from the Sim and any devices. Option-clean the build folder. Then rebuild.

If the problem persists then what does the Build phases say for these files? Are they really in your target?

Great Idea! I'll give this a try and will advise...

AZAstro
Sep 29, 2012, 02:18 AM
If you're really getting back NULL then that's a simple problem. I recommend you start by deleting your app from the Sim and any devices. Option-clean the build folder. Then rebuild.

If the problem persists then what does the Build phases say for these files? Are they really in your target?

That was it. Those files didn't show up in the bundle. Dragging and dropping the files into the project navigator is supposed to do that automatically, but it didn't do its job. BUG!@

I added the files manually (in BUILD PHASES) and then everything worked fine.

Thanks for a great suggestion. It was easy once I was headed in the right direction!

dejo
Sep 29, 2012, 02:52 PM
Dragging and dropping the files into the project navigator is supposed to do that automatically, but it didn't do its job. BUG!@

It does, if you have the "Copy items into destination group's folder (if needed)" option checked. I doubt you found a bug with Xcode.