Dizzy Dizzy - 1 month ago 14
iOS Question

iOS Control Sound Volume

I'm playing sounds like this:

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
..
..
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"ClickSound" ofType:@"wav"];

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);


It seems to take it's volume from 'Ringer', But when I use the physical volume button it control the 'Volume' (so I can 'Mute' the volume - but I still hear the sound).

I want to control the right volume and I don't want it to play when it's muted (BTW - when I use the mute toggle it works, and I don't hear the sound).

How can I fix it?

Answer

AudioServicesCreateSystemSound applies only to the ringer volume.

You can use AVAudioPlayer for this. Here is some sample code:

AVAudioPlayer *buttonClick=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"buttonClick"] ofType:@"mp3"]] error:NULL];
[buttonClick prepareToPlay];
[buttonClick play];
Comments