I am making a small iOS game, and have finished everything, except the sound. I have checked the file formats, tried .wav and .aiff and none of them work. The related code is as follows:
.h file:
#import <AudioToolbox/AudioToolbox.h>
@public SystemSoundID mBeep;
NSString* path;
NSURL* url;
.m file:
path = [[NSBundle mainBundle]pathForResource:@"hit" ofType:@"wav"];
url = [NSURL URLWithString:path];
AudioServicesCreateSystemSoundID((CFURLRef)url, &mBeep);
AudioServicesPlaySystemSound(mBeep); //Gets called, doesn't play
If I use one of the undocumented ids, like 1103 with AudioServicesPlaySystemSound, that sound plays fine.
I am quite sure my files are fine, as the same .wav file works perfectly on Android, VLC, iTunes etc.
Does anyone know what I am doing wrong?
The most likely culprit is the encoding on the WAV. As indicated in this answer, iOS only supports certain codecs, and the WAV you’re trying to play probably uses an unsupported one.
Like that answer says, the easiest solution will be to use the command-line
afconverttool to (losslessly) convert your WAV to Apple’s default CAF format and use that instead:Another possibility—and the actual problem the OP had—is that the file doesn’t exist and/or
pathForResourceisn’t successfully finding it, which would be indicated bypathbeingnil.