views:

1097

answers:

2

I have an app with various views. The main menu does not have any sounds, but the next views play sounds using the AVAudioPlayer Class.

So when someone launches the app and is in the main menu, if he changes the volume on his device, he actually changes the "Ringer" volume. If he proceeds to the other views (where we have sound), when he changes the volume on the device he changes the volume of the game, not the ringer.

Is there a way to have them change the app volume every time, from the beginning of my app, before I create any instances of AVAudioPlayer?? (some misunderstood that. What I mean is that square that the OS overlays on the screen every time you press the volume buttons. If you are on the home screen, you change the "Ringer". When you are in a game, you change the app's volume).

PS: I initialize the AudioSession on my main menu but that doesn't make any difference. The only hack i have found is to actually create an instance of AVAudioPlayer on my main menu and set it to "preparedToPlay". But I would rather hear what the others are doing (a proper solution).

A: 

There is a class called MPVolumeView which is used for this purpose. Here's a good link explaining the use of MPVolumeView

Adding MPVolumeView to your view will create an iPod-like volume slider which will change when you change the volume using the rocker buttons. The value will automatically be used by the AVAudioPlayer class.

To keep track of volume without the slider

The rocker buttons by default change the app volume only when some audio is playing. There is a workaround to this problem - this SO thread discusses some workarounds.

lostInTransit
I think my question is misunderstood... I probably need to rephrase it... I don't want to give the users a slider to change the volume. But when they do change the volume (from the buttons on the side of the device) I want that to change the "App Volume" instead of the "Ringer Volume".
Dimitris
Ok. see edited answer
lostInTransit
Ah, ok. So my *hack* is actually what others are doing too...Thanks.
Dimitris
+1  A: 

The volume buttons will affect the ringer volume if your application isn't currently playing audio. Some apps work around this by playing a silent audio file. Carefully consider how you do this, though - is adjusting the apps volume when it's not actually playing something actually useful?

It seems like you ought to be able to do what you want by modifying the AudioSession properties, but I haven't figured out how (if it is possible).

Mark Bessey
That's what I thought, but for my app i think the default AudioSession is the best one, so I don't modify anything.As for your first paragraph, it's very useful to be able to change the volume of audio even if it's not currently playing (imagine an app that plays short beeps - you would have to time your finger with the beeps to be able to lower their volume...).
Dimitris