Sound doesn't play on iOS
I am making a small iOS game and finished everything except sound. I checked the file formats, tried .wav and .aiff and none of them work. The related code looks like this:
.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 identifiers, such as 1103
c AudioServicesPlaySystemSound
, it sounds great.
I'm pretty sure my files are fine as the same .wav file works fine on Android, VLC, iTunes, etc.
Does anyone know what I am doing wrong?
source to share
The most likely culprit is the WAV encoding. As stated in this answer , iOS only supports certain codecs, and the WAV you are trying to play is probably using an unsupported one.
As in this answer, the simplest solution would be to use a command line tool afconvert
to (losslessly) convert your WAV to Apple's default CAF format, and use instead:
afconvert sound1.wav sound1.caf
Another possibility - and the actual problem the OP had - is that the file doesn't exist and / or pathForResource
doesn't find it, which would be indicated path
nil
.
source to share