Override ringer volume in iPhone apps

12,170

Solution 1

I have found the solution to what I thought would be a common problem. So here is how your app can have its own volume, and not mess with the user's ringer volume, even if you are only playing sounds as System Sounds.

You have to import the AVFoundation framework and in an object that stays loaded the whole time your app runs (or view, or the app delegate) you have initialize a AVAudioPlayer, give it a file to play and set it to "prepareToPlay" it...

This is what i did in my main View (that is used to load other views as subviews): in the header file:

#import <AVFoundation/AVFoundation.h>

@interface MainViewController : UIViewController {
    AVAudioPlayer *volumeOverridePlayer;
}

@property (nonatomic, retain) AVAudioPlayer *volumeOverridePlayer;

In the implementation file:

    @synthesize volumeOverridePlayer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    volumeOverridePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"something" ofType:@"caf"]] error:nil];
    [volumeOverridePlayer prepareToPlay];
//...
}

Just leave the player prepared to play your file and enjoy your own volume control without having to play the sounds through it! And of course, remember to release it in dealloc.

Solution 2

From what I can see in Apple's documentation, there are two issues here. 1. Application and the OS/hardware volume can only be controlled by the hardware/user. 2. You are playing a system sound. I believe the system sound is governed by the OS, so you're out of luck here, unless you are playing actual sound files. If you can use sound files, then take a look here.

Good luck!

Share:
12,170
Dimitris
Author by

Dimitris

Developer, manager, maker of things. SOreadytohelp

Updated on June 04, 2022

Comments

  • Dimitris
    Dimitris almost 2 years

    I have built an app that plays lots of sounds the easy way:

    AudioServicesPlaySystemSound(someSoundID);
    

    When I use the device volume buttons to increase or decrease the volume, the volume I actually change is the phone's ringer volume. So if you decrease it and exit the app then your phone will ring quietly...

    Is there an easy way to override this and actually change my application's volume?