Reputation: 327
I'm looking to play a sound on my iPhone (iOS 6.1) depending on pressed buttons.
After converting to aif, caf, wav and compressed formats, I could not get sound on the device. Sound volumes have been checked, device has been rebooted, no uppercase letters are included in filenames.
I read many topics about this problem and could not find a proper solution, which means my problem is either more complex or plain stupid - surely plain stupid.
Here is my code to play the sound:
// Plays sound of given filename
- (void) playSound:(NSString*)name {
SystemSoundID sound = [self createSoundID:name];
AudioServicesPlaySystemSound(sound);
NSLog(@"file played: %@",name);
}
// Creates a playable sound out of filename
-(SystemSoundID) createSoundID:(NSString*) name
{
NSString *path = [NSString stringWithFormat: @"%@/%@",
[[NSBundle mainBundle] resourcePath], name];
NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
return soundID;
}
Upvotes: 3
Views: 675
Reputation: 327
The problem was that the method calling playSound was not called with an NSTimer. It seems that for the sounds to play, they must be called by the main thread of the application (which seems legit...).
So I modified the app so that the main thread would call those sounds and voila, it worked on the device as well. The code above was correct.
Upvotes: 0
Reputation: 6092
If the sound works fine on simulator but not in real device that means you should turn on your device's Ringer and Alert Settings. you should also check the mute button, if its' on or not.
Upvotes: 2