21

I have this code:

-(void)createAndPlaySoundID: (NSString*)name 
{
    NSString *path = [NSString stringWithFormat: @"%@/%@", [[NSBundle mainBundle] resourcePath], name];
    
    NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
    
    AudioServicesPlaySystemSound(soundID);
}

I call this like so:

[self createAndPlaySoundID: @"mySound.caf"];

This works great, however I have no control over the volume. I simply want the volume to be controlled by the user using the standard physical volume buttons on their iPad.

If I put this volume down to say 1, it plays, great, but then if I whack the device volume up to max its the same volume.

halfer
  • 19,824
  • 17
  • 99
  • 186
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • Have you tried with a different, long sound so you can test changing the volume while it's playing? Apple's documentation says "Sounds play at the current system audio volume, with no programmatic volume control available" for AudioServicesPlaySystemSound so it's odd if it isn't doing what Apple says it should. – Christian Jun 01 '12 at 19:06
  • Did you find any solution to this? I'm facing the same problem. – tommys Sep 03 '12 at 10:54
  • 1
    update: I found out that the volume was infact adjusted when using the volume buttons - my problem was really that the sound was too amplified in itself so decreasing the volume simply didn't affect the volume that much. I solved this by adjustng the sounds own amplification in audacity. – tommys Sep 03 '12 at 11:33
  • [OFF TOPIC] You should just use this to get the URL: `NSURL * filePath = [[NSBundle mainBundle] URLForResource:name withExtension:nil];` – hypercrypt Sep 22 '13 at 12:57

1 Answers1

35

I was also bit by a similar problem on this topic.

My problem was that there was a global setting that I was unaware of.

In the Settings app, under

General > Sounds > Ringer and Alerts

If 'Change with Buttons' is set to Off, then sounds using AudioServicesPlaySystemSound() will always be played at full volume (yet other sound API's such as AVAudioPlayer will respect the volume of the device).

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
Jon Brooks
  • 2,472
  • 24
  • 32
  • Thanks, In that case I have found that Sounds using AudioServicesPlaySystemSound() will be played at volume showing in disabled slider (not full volume). – DareDevil Mar 14 '14 at 06:53