Ethan G Ethan G - 1 year ago 268
iOS Question

How can I play a system sound at lower volume in Objective-C?

I am making an iOS 8 Objective-C app (deployed on my iPhone 5) and I'm using this code to play a sound through the phone from the app:

@property (assign) SystemSoundID scanSoundID;


- (void)someFunction {


//Play beep sound.
NSString *scanSoundPath = [[NSBundle mainBundle]
pathForResource:@"beep" ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)scanSoundURL, &_scanSoundID);



This code works fine, but the beep.caf sound is quite loud. I want to play the beep sound at 50% volume (without changing the volume of the iPhone). In other words, I don't want to touch the iPhone's actual volume, I just want to play the sound with less amplitude so to speak.

How can I accomplish this (preferably with Audio Services like I'm using now)?


After trying to implement the answer from Louis, this code is not playing any audio (even though my phone is not on silent and my volume is turned up):

NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL
player.volume = 0.5;
[player play];

Answer Source

From the Multimedia Programming Guide under "Playing UI Sound Effects or Invoking Vibration Using System Sound Services":

In addition, when you use the AudioServicesPlaySystemSound function:

  • Sounds play at the current system audio volume, with no programmatic volume control available

So, it doesn't appear to be possible according to the Apple Docs. But the AVAudioPlayer class lets you do it via its volume property.

In your current code, all you'd have to do is add

AVAudioPlayer *newPlayer = 
        [[AVAudioPlayer alloc] initWithContentsOfURL: scanSoundURL
                                               error: nil];
[newPlayer play];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download