iPhone volume changed event for volume already max - iphone

I'm using
AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume,
audioVolumeChangeListenerCallback, self);
to detect volume changes so i can take a picture with the volume rockers but if the phone is already at 100% i don't get an event. Is there a way to detect volume rocker pressed when the volume doesn't actually change?

I found a good answer with full source code here:
When the user presses the volume buttons in your app, you don’t want
the system volume to change. It would suck to have them taking
pictures and turn up their ringtone volume all the way or something.
So how do I pull that off? This is pretty hacky but it works.

Well you could set the volume lower after you've registered a volume button press to take a picture. That way the volume never reaches 100% and you can continue getting callbacks. Make sure to lower the volume by more than the volume changes in a single press of the volume button.
You will want to be careful to use some method to prevent receiving multiple notifications at once, but you are likely already doing that if you only take one photo per "button press".
I think I'm going to implement this feature (didn't think it was needed, but now I like the idea) in my app, and I'll post some code if I get it working, let me know if you get yours working as well.

Related

Take photos using volume buttons

Is it possible to take photos using the volume controls on the iPhone?
Ideally, the takePicture() method would be called when the volume button is pressed.
Add an event listener.
Titanium.Media.addEventListener('volume', function(e) {
Ti.Media.takePicture();
});
Make sure the event listener is added when the camera is already shown. Docs here: http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Media-method-takePicture
Old, but still relevant: program access to iPhone volume buttons
Also consider adding better tags to your question, objective c, ios, xcode, etc.

When hardware volume switch sets off, any way to detect off state and play audio regardless of off.

I see only method to make sound in volume off state is AudioServicesPlayAlertSound. Volume off mean that it is off by lateral buttons(hardware button) rather than ringer switch in setting. It seems that we cannot control volume up,down and duration programmatically using AudioServicesPlayAlertSound. I would want to make an repeat alarm programmatically even if system volume is in muted.
thanks
Not sure you can do that, I think you "legally" can't.
This link might help you solve it:
play-sound-on-iphone-even-in-silent-mode
And this is a to check the silent button state and vibrete instead:
how-to-play-sound-file
Hope that helps.

Increase volume programmatically

i want in my app to increase and decrease the app volume programmatically from the code and not to use MPVolumeView.
You cannot change the volume programmatically, and that's a deliberate design choice. Imagine an app that constantly sets the volume to the highest level, that would not only be annoying but could even damage your ears if you're wearing in-ear headphones.
You should show an MPVolumeView to the user so he can change the volume himself. You can walk its subview hierarchy and search for the UISlider and adjust its appearance like with any normal UISlider. This way you can adapt the MPVolumeView to your app design.
Officially: You can't.
If you really want to do so, link against Celestial.framework and use its controllers to change the volume - that is how SpringBoard and friends do it. I don't remember how to do it, but with a bit of research you should be able to find the answer. This will get your app rejected. If you're developing for a jailbroken device, this is the way to go.
If you want to go deeper you can do it via the vTable of the appropriate CoreAudio service. You also need Celestial.framework for that. And if you want to go even deeper, kill mediaserver and make your own implementation which should occupy your for the next few months.

How to get the device volume values to my app

I am using volume in my app using MPMusicPlayerController with plus and minus buttons for increasing and decreasing the volume(for increasing and decreasing indicates through horizontal images). While pressing the buttons(user defined) volume works good, if i use the device volume button volume is adjusting but its not syncing with app volumes button, i.e the images for increasing and decreasing are not getting called.
Simply, how can get the device volume values to my app ?
You can find all the answers on this thread. It also has some example code. Look at Sandy's answer!

Standard way of adding Sound to an iPhone app (global volume indicator)

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