I want to play a system sound but I don't hear anything:
- (void) play_system_sound
{
NSLog(@"Play system sound");
SystemSoundID soundID;
NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
NSLog(@"path = %@", path );
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
NSLog(@"Play system sound DONE");
}
I don't hear anything. What am I doing wrong?
I don't know if you have solved the problem, but for me, on iOS 7, sometimes the
(__bridge CFURLRef)
casting won't work, in which case, if you print out the result of(__bridge CFURLRef)[NSURL fileURLWithPath:path]
, it will be0x0
. This leads to failure ofAudioServicesCreateSystemSoundID()
. The return value of the function is type ofOSStatus
. If it fails, the function will return-50
, which means one or more parameters are invalid. (If it executes successfully, it returns0
.)I solved this by using the C style function getting the path of the file:
After
AudioServicesPlaySystemSound(soundID)
,don't callAudioServicesDisposeSystemSoundID(soundID)
immediately.Try this :