有没有办法检测耳机的播放/暂停按钮点击?检测耳机按钮点击iPhone SDK
我设法检测音量按钮点击使用:
AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self);
但我不能找到中间的按键的AudioSessionProperty。有什么办法做到这一点?
有没有办法检测耳机的播放/暂停按钮点击?检测耳机按钮点击iPhone SDK
我设法检测音量按钮点击使用:
AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self);
但我不能找到中间的按键的AudioSessionProperty。有什么办法做到这一点?
从您的应用以外完成的所有事情都被视为“远程事件”。如果您双击主页按钮并按下播放/暂停按钮,则相当于按下耳机上的播放/暂停按钮(对于下一个双击和前一个三拍)。
以下是关于event handling of remote events for iOS的指南。
就个人而言,我喜欢继承的主窗口(UIWindow
)和重写sendEvent:
方法,这样我就可以更直接地管理它:
- (void)sendEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl)
{
// Do stuff here
}
else
{
// Not my problem.
[super sendEvent:event];
}
}
希望帮助,枚举的中央按钮的事件是UIEventSubtypeRemoteControlTogglePlayPause
。
Can的答案很好,但我认为它已经过时了。
现在您需要继承UIApplication。
代码main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "MyUIApplication.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(
argc,
argv,
NSStringFromClass([MyUIApplication class]),
NSStringFromClass([AppDelegate class]));
}
}
代码MyUIApplication.m
:
@implementation MyUIApplication
- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
// Check event.subtype to see if it's a single click, double click, etc.
} else {
// Not my problem.
[super sendEvent:event];
}
}
@end
代码AppDelegate.m
:中
内- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
通话[application beginReceivingRemoteControlEvents];
谢谢可以。我通过使用方法找到了另一个解决方案:“remoteControlReceivedWithEvent:”。解决方法在这里讨论:http://www.iphonedevsdk.com/forum/iphone-sdk-development/44433-there-way-respond-clicks-headphone-buttons.html – MCO
@我可以试试你的解决方案和当我按下耳机上的按钮时,不会调用sendEvent UIEventTypeRemoteControl事件类型。是否还有其他一些设置可以帮助你实现这个目标?这个子类正在工作,因为我确实发现了一些其他事件。 –