当用户调整铃声音量时,你能在iPhone上触发功能吗?

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.

#0

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.

#1

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?

#2

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.

推荐文章

是吗??``操作员使用短路?

是吗??``操作员使用短路?

推荐文章

SPIM中C++代码到汇编的转换

SPIM中C++代码到汇编的转换

推荐文章

如何用c++编写可移植代码?

如何用c++编写可移植代码?

推荐文章

关于类中字符串的Getter或Const的设计建议-其他人在做什么?

关于类中字符串的Getter或Const的设计建议-其他人在做什么?

推荐文章

在Coldfusion 9上使用cflock排队

在Coldfusion 9上使用cflock排队

推荐文章

在关键部分时是否可以进行上下文切换?

在关键部分时是否可以进行上下文切换?

推荐文章

联合对或

联合对或

推荐文章

Visual Studio 6 SVN插件

Visual Studio 6 SVN插件

推荐文章

FT-EDSL中的Y-组合子

FT-EDSL中的Y-组合子

推荐文章

我怎么能轻易地修正过去的承诺?

我怎么能轻易地修正过去的承诺?

推荐文章

我应该如何处理Ruby on Rails中的估计日期?

我应该如何处理Ruby on Rails中的估计日期?

推荐文章

在桌面和浏览器/webapp之间共享数据的任何(或最佳)方法?

在桌面和浏览器/webapp之间共享数据的任何(或最佳)方法?

推荐文章

作为Windows服务运行Tomcat Servlet

作为Windows服务运行Tomcat Servlet

推荐文章

grails help命令的行为很奇怪

grails help命令的行为很奇怪

推荐文章

在目标安装上注册dll

在目标安装上注册dll

推荐文章

不重要的bug应该被修复,这样它们就不会分散开发人员的注意力吗?

不重要的bug应该被修复,这样它们就不会分散开发人员的注意力吗?