开发者

Can you trigger functions on the iPhone when the user adjusts the ringer volume?

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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜