Reputation: 31
This is my current code
void playSound(NSString* myString) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
NSString *string = myString;
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle,
(__bridge CFStringRef) string, CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
It works, but it doesn't let you change the volume on the device or mute it with the mute switch. What is the simplest way to enable these features? Thanks.
Upvotes: 3
Views: 704
Reputation: 40211
Why don't you use AVFoundation? If all you want is to play simple sound effects, it would be a much better choice as it is a higher level API.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
// Assuming audioPlayer is an ivar.
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
AVAudioPlayer
should respect the volume settings of the user.
Upvotes: 0