views:

2041

answers:

3

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?

+1  A: 

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
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
+2  A: 

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.

Dimitris
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
A: 

I have the problem, that i record some sound and so i cant have a AVplayer running... is there a methode to get the ringtone volume? then i could set the mp3 volume to the same setting as the ringtone volume..