Dizzy
Dizzy

Reputation: 179

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?

Upvotes: 5

Views: 795

Answers (3)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

Probably you should register for Volume button's Call backs using below code,

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(volumeChanged:)
     name:@"AVSystemController_SystemVolumeDidChangeNotification"
     object:nil];
}

- (void)volumeChanged:(NSNotification *)notification
{
    float volume =
    [[[notification userInfo]
      objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
     floatValue];
}

Hope it will helps . Happy coding :)

Upvotes: 0

Erez
Erez

Reputation: 2655

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];

Upvotes: 2

Avi
Avi

Reputation: 7552

There's no way for you to control the volume in code. You can provide a UI control to let the user change the volume.

Audio is managed by categories, and behavior differs with respect to respecting the hardware settings.

The Ambient category will honor the mute switch; the others won't.

No category allows you to determine the position of the mute switch.

Upvotes: 0

Related Questions