Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 453
Dizzy Avatar asked Oct 31 '22 12:10

Dizzy


1 Answers

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];
like image 194
Erez Avatar answered Nov 15 '22 00:11

Erez