1

In iOS 5, the volume-up button will now take a photo in the camera app, and on a UIImagePickerController instance where .showsCameraControlls == YES. Happy days.

However, when I set showsCameraControlls to NO, and supply my own (which in turn triggers takePicture method), the volume-up button will no longer work. How can I detect the volume event while the UIImagePickerController is showing?

The old way to detect volume changes was like so:

AudioSessionSetActive(true);
[[NSNotificationCenter defaultCenter]
   addObserver:self
   selector:@selector(volumeChanged:)
   name:@"AVSystemController_SystemVolumeDidChangeNotification"
   object:nil];

I added this code to my application delegate. Strangely volumeChanged: is not triggered until after I show the UIImagePickerController for the first time. More importantly, it isn't triggered while the UIImagePickerController is visible (nor is the usual volume HUD shown), I guess since Apple disabled it & hijacked the event.

So once again, is there any way to detect the volume-up button event, while the UIImagePickerController is being displayed, when using custom camera controls, for the purpose of taking a photo?

If you're wondering why I need to use custom camera controls, it is because I want the ability to take multiple photos, which the standard camera controls do not allow.

William Denniss
  • 16,089
  • 7
  • 81
  • 124

2 Answers2

4

On iOS 8 you can add an observer to the notification _UIApplicationVolumeUpButtonDownNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(volumeChanged:)
                                      name:@"_UIApplicationVolumeUpButtonDownNotification"
                                      object:nil];

If you are using UIImagePickerController, I was able to capture the event and use it to call TakePicture with a custom view.

On top of that, UIImagePickerController ensures that pressing volume up won't change the volume.

I'm not sure if Apple would approve an app listening to that notification; this seems to be the cleanest approach.

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
2

Try using the AVCapture APIs instead of UIImagePicker. Here's a tutorial:

http://www.musicalgeometry.com/?p=1273

It's a lot lower level and it's harder to use, but it shouldn't block the volume controls.

You might also want to try a trick like playing a silent audio file to enable the volume controls during image capture.

Update: I also found this tutorial on using the volume button for camera shutter:

http://ios.biomsoft.com/2011/12/07/taking-control-of-the-volume-buttons-on-ios-like-camera/

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • Thanks, I'm more keen on the high level stuff at the moment, the low level stuff is kinda overkill just to add this one feature. Your tutorial link looks promising, I'll check it out. – William Denniss Feb 02 '12 at 14:16
  • I tried the tutorial, it works – but not while UIImagePickerController is visible. – William Denniss May 04 '13 at 11:54