Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AZAstro

macrumors newbie
Original poster
Sep 22, 2012
5
0
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..
Code:
// 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.
 
Last edited by a moderator:

nickculbertson

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
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

macrumors newbie
Original poster
Sep 22, 2012
5
0
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors newbie
Original poster
Sep 22, 2012
5
0
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors newbie
Original poster
Sep 22, 2012
5
0
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

macrumors newbie
Original poster
Sep 22, 2012
5
0
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 

Attachments

  • Screen Shot 2012-09-29 at 1.50.55 PM.png
    Screen Shot 2012-09-29 at 1.50.55 PM.png
    23.1 KB · Views: 212
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.