Question

In my app I will play a system sound when it received a signal of specific frequency. To detect frequency I used the PitchDetector. Then I looked to the internet to see how to play the iOS system sound and I found this. I wanted to play the sound called sms_alert_circles.caf, so in method - (void)viewDidLoad I wrote this code:

NSURL *fileURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds/Modern/sms_alert_circles.caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)fileURL,&soundID);

then in the method who detect the frequency of the audio signal I put this code:

AudioServicesPlaySystemSound(soundID);

but if I try to run this code on my iPhone nothing happened. It recognize the frequency, but it don't execute the sound, what's wrong in my code? Can you help me?

Obviously I turned on the sound of my device

Was it helpful?

Solution

Maybe your path is wrong, it should be path to a file inside the app's bundle.

Add your sms_alert_circles.caf file to your xcode project. Check that file is in "Copy Bundle Resources" list in "Build Phases" of project. Then change following line:

NSURL *fileURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds/Modern/sms_alert_circles.caf"];

to

NSString *soundPath = [[NSBundle mainBundle]
                           pathForResource:@"sms_alert_circles.caf"
                           ofType:nil];
NSURL *fileURL = [NSURL URLWithString:soundPath];

Also you can try to use AKSystemSound library to play sound via almost one line of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top