Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 22, 2012, 03:34 PM   #1
AZAstro
macrumors newbie
 
Join Date: Sep 2012
This drives me nuts - CFBundleCopyResourceURL returns NULL

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 balamw; Sep 22, 2012 at 05:48 PM. Reason: CODE tags
AZAstro is offline   0 Reply With Quote
Old Sep 22, 2012, 06:14 PM   #2
nickculbertson
macrumors regular
 
Join Date: Nov 2010
Location: 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
__________________
My App Blog --->
nickculbertson is offline   0 Reply With Quote
Old Sep 22, 2012, 07:27 PM   #3
AZAstro
Thread Starter
macrumors newbie
 
Join Date: Sep 2012
Quote:
Originally Posted by nickculbertson View Post
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.
AZAstro is offline   0 Reply With Quote
Old Sep 23, 2012, 12:57 PM   #4
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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
PhoneyDeveloper is offline   0 Reply With Quote
Old Sep 27, 2012, 12:05 AM   #5
AZAstro
Thread Starter
macrumors newbie
 
Join Date: Sep 2012
Quote:
Originally Posted by PhoneyDeveloper View Post
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...
AZAstro is offline   0 Reply With Quote
Old Sep 27, 2012, 08:21 AM   #6
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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?
PhoneyDeveloper is offline   0 Reply With Quote
Old Sep 29, 2012, 01:35 AM   #7
AZAstro
Thread Starter
macrumors newbie
 
Join Date: Sep 2012
Quote:
Originally Posted by PhoneyDeveloper View Post
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 is offline   0 Reply With Quote
Old Sep 29, 2012, 03:18 AM   #8
AZAstro
Thread Starter
macrumors newbie
 
Join Date: Sep 2012
Quote:
Originally Posted by PhoneyDeveloper View Post
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!
AZAstro is offline   0 Reply With Quote
Old Sep 29, 2012, 03:52 PM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by AZAstro View Post
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.
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-09-29 at 1.50.55 PM.png
Views:	65
Size:	23.1 KB
ID:	364836  
__________________
dejo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Problem with mail app and gmail that drives me nuts Dowjohnny Mac Applications and Mac App Store 12 Apr 8, 2014 01:14 PM
Improper texting...drives me nuts. Who's with me? thedeejay Community Discussion 65 Mar 29, 2014 08:19 AM
naming external drives, hard drives, thum drives for best compatability Sossity Mac Peripherals 1 Apr 5, 2013 04:11 AM
Resolved: self.splitViewController... returns NULL? ArtOfWarfare iPhone/iPad Programming 5 Aug 7, 2012 11:51 PM

Forum Jump

All times are GMT -5. The time now is 01:07 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC