1

I updated to OS X Lion 10.7.3 and to Xcode 4.3.2. I am unable to play a sound on the iPhone Simulator or on the iPhone device with the following code. I am able to play a sound using the same code on Snow Leopard and Xcode 4.2. I have no idea why I am not able to hear the sound in 4.3.2. Can any one help?

.h file

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>


@interface soundViewController : UIViewController {
}

-(IBAction)playSounds:(id)sender;

@end

.m file

-(IBAction)playSounds:(id)sender {

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"E", CFSTR("caf"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
Kinematic
  • 13
  • 2
  • Is `soundFileURLRef` `NULL`? Did you add `E.caf` to your target's resources? – Kurt Revis Apr 08 '12 at 07:28
  • Also see this [similar question](http://stackoverflow.com/questions/7856896/couldnt-play-system-sound-after-switching-to-ios-5). – Kurt Revis Apr 08 '12 at 07:29
  • I put NSError *error NSLog(@"%@", error, userInfo) and it returned null for both but plays on the iMac E.caf is in my target resources. – Kinematic Apr 08 '12 at 07:34
  • That's not how you check for errors. An NSError does not mysteriously materialize out of nowhere -- it needs to be returned by some method. None of the CF or AudioServices functions that you are calling will return an NSError. – Kurt Revis Apr 08 '12 at 07:40

1 Answers1

0

XCode 4.3 sometimes excludes files from the copy resources list for no reason. Verify that the file is included in your target. Even if you add it, sometimes it does not become a resource for the target. Click on the file and choose the left most tab of the property inspector, and make sure the checkbox next to your target is checked in the "Target Membership" section.

borrrden
  • 33,256
  • 8
  • 74
  • 109