1

I would like to bring up the Camera in my app, and let the user take a photo by pressing the hardware volume buttons on the phone.

Is this possible? Can I trigger a function when the users changes the volume, and can I then programatically trigger the camera in this function?

A link or code snippet would be much appreciated.

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
  • 3
    Somehow, I think they're going to throw the HIG in your face if you try this. – Frank Krueger Nov 10 '09 at 00:39
  • 2
    I kind of doubt apple would accept that behavior, if you plan on submitting to the store. The physical volume controls aren't intended to be used differently in different apps or contexts. I don't think there are any notifications in the public api for volume changes. but take a look at this post http://stackoverflow.com/questions/772832/program-access-to-iphone-volume-buttons – wkw Nov 10 '09 at 00:42

3 Answers3

8

I second the comments by Frank Krueger and wkw. From a user experience point of view, this sounds like a very bad idea. If you want to try it anyway, you could try this:

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];

The system will now generate a MPMusicPlayerControllerVolumeDidChangeNotification whenever the volume changes. You can register yourself as an observer for the notification with -[NSNotificationCenter addObserver:selector:name:object:]. This is not directly tied to the volume control buttons, though. For instance, it's also possible that the user inserted or removed headphones and the OS adjusted the volume according to the last setting for this configuration).

Also, I believe the volume notification is only for changes of the music volume and not the system/ringer volume. So it would only be posted when music is playing. But I am not certain about this.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
0

To get volume change notifications you can define a callback function:

    void listenerCallback (void *inUserData, 
                           AudioSessionPropertyID inPropertyID, 
                           UInt32 inPropertyValueSize, 
                           const void *inPropertyValue)

and then register it with:

AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume,            
                                propertyListenerCallback, 
                                userData);                               

This of course presumes you're initializing your application's AudioSession before this.

However, using the volume buttons for anything else than something volume related puts your app on the fast lane to being rejected.

Mihai Damian
  • 11,193
  • 11
  • 59
  • 81
0

Interesting how this is such a bad idea until Apple implements it. Does Andrew get any compensation from Apple for coming up with this idea before them?

amergin
  • 3,106
  • 32
  • 44