2

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?

Dimitris
  • 13,480
  • 17
  • 74
  • 94

2 Answers2

4

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Dimitris
  • 13,480
  • 17
  • 74
  • 94
  • 1
    Coming back a long time after I posted this, want to say that I found out that it only worked only for decreasing the volume and not increasing it... Anyway, I have been using AVAudioplayers ever since to play my sounds and rarely (if ever) play them through alerts. – Dimitris Oct 22 '09 at 16:56
  • it is an old topic - but i wonder maybe the new sdk has some changes: i want my application to play a tone from file regardless of the ringer volume - and not even allowing the user to play with the volume or changing it. i want to be as loud as possible without any interference from the user (lowering the volume of the ringer or even putting it in mute mode - i still want to play the tone as loud) - what can i do? my code is using AvAudioPlayer and it is not working for me... – Itay Levin Dec 01 '10 at 07:55
  • Try AudioServicesPlaySystemSound(someSoundID); Alert sounds are using the phone ring volume I believe. – Dimitris Dec 01 '10 at 15:57
1

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!

Simon
  • 1,746
  • 1
  • 13
  • 21
  • 2
    I am playing sound files, I'm just playing them as System Sounds. Apple proposes to play short sounds (less than 30 seconds) this way. If it can't be done this way, I might have to do it the other way you are proposing. Thanks :) – Dimitris Jun 25 '09 at 09:52