Question

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).

Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top