I have searched around the website and tried all possible options which have been suggested, to make the sound work in my iPhone app which works on the simulator.
Specifically I have tried what's been suggested here: Sound working in emulator, not in real iPhone
In addition I have tried running the both ".wav" as well as ".caf" file from within my app, with the following code:
CFURLRef soundFileURLRef = NULL;
soundFileURLRef = CFBundleCopyResourceURL (CFBundleGetMainBundle (),
CFSTR ("applause"),CFSTR ("wav"),NULL );
NSLog(@"Clap Path: %s", CFURLGetString(soundFileURLRef));
AudioServicesCreateSystemSoundID (soundFileURLRef,&clappingFileID_ );
AudioServicesPlaySystemSound (clappingFileID_);
Based on the other suggestions I also changed the code to following:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *clapPath = [documentsDir
stringByAppendingPathComponent: @"applause.wav"];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:clapPath]) {
NSString *defaultClapPath = [[NSBundle mainBundle]
pathForResource:@"applause" ofType:@"wav"];
if (defaultClapPath) {
[fileManager copyItemAtPath:defaultClapPath toPath:clapPath error:NULL];
}
}
// NSString *clapPath = [[NSBundle mainBundle] pathForResource:@"applause" ofType:@"caf"];
CFURLRef clapURL = (CFURLRef ) [NSURL fileURLWithPath:clapPath];
AudioServicesCreateSystemSoundID (clapURL, &clappingFileID_);
NSLog(@"calling initSounds..");
AudioServicesPlaySystemSound (clappingFileID_);
Both the above code pieces work for the simulator but not for the iphone.
Also checked the .wav file properties by doing getInfo. it says: Bit rate: 176 kbps Sample rate: 11.025 kHz Sample size: 16 bit Size 62 Kb
Also tried running .wav file on the iPhone directly outside of the context of app by adding it thru itunes and this works fine. which indicates to me that the codecs are there on the iPhone for the given .wav file and that its not corrupted.
My phone OS is 4.1 and its iPhone 3GS. I have compiled my application for OS 3.2 or above upto 4.2