iPhone SDK 检测耳机按钮点击
有没有办法检测耳机的播放/暂停按钮点击?
我设法使用以下方法检测音量按钮点击:
AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self );
但我找不到中心按钮的 AudioSessionProperty。有什么方法可以做到这一点?
Is there a way to detect the headset's play/pause button click?
I managed to detect the volume buttons clicks using:
AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self );
But I can't find an AudioSessionProperty for the center button. What's the way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从应用程序外部完成的所有操作都被视为“远程事件”。如果您双击主页按钮并按此处的播放/暂停,则相当于按耳机上的播放/暂停按钮(双击下一个按钮,三次点击上一个按钮也是如此)。
这是有关事件处理的指南iOS 远程事件。
就我个人而言,我喜欢子类化 MainWindow (
UIWindow
) 并重写sendEvent:
方法,这样我就可以更直接地管理它:希望有帮助,事件的枚举中央按钮是
UIEventSubtypeRemoteControlTogglePlayPause
。Everything that's done from outside your app is considered a "Remote Event". If you double-tap the Home button and press Play/Pause there, it's the equivalent of pressing the play/pause button on the headset (Same for double tapping for next, and triple tapping for previous).
Here's the guide on event handling of remote events for iOS.
Personally, I like subclassing the MainWindow (
UIWindow
) and overriding thesendEvent:
method, so I can manage it more directly:Hope that helps, the enum for the event of the central button is
UIEventSubtypeRemoteControlTogglePlayPause
.尝试了以上所有方法,但遗憾的是现在似乎都不起作用。
然后我看了一眼
beginReceivingRemoteControlEvents
并发现了这个然后查看
MPRemoteCommandCenter
并最终进入MPRemoteCommand
文档页面。好消息是有这个例子:
现在,如果我们想读取中间按钮,我们可以这样做:
这有效,我设法检测到耳机的中间按钮。
笔记:
我注意到有不同的行为取决于我们在上面放置此类代码的位置。
也就是说,当我将其放入 View Controller 中时,报告的事件是相同的,而当我将其放入 AppDelegate 的 didFinishLaunching 中时,报告的事件是不同的。无论哪种方式,事件都会被检测到。
Tried all above but sadly now none seems to work.
Then I took a peek at
beginReceivingRemoteControlEvents
and found thisThen checked out
MPRemoteCommandCenter
and finally ended up inMPRemoteCommand
documentation page.Good thing is there is this example:
Now if we want to read middle button we can do:
This works and I managed to get the headphone's middle button detected.
Note:
I noticed there is different behaviour that depends on where we put such code above.
That is when I put in View Controller the reported events are identical and when I put it in AppDelegate's didFinishLaunching the reported events are different. Either way the event is detected.
Can的答案很好,但我认为它已经过时了。
现在您需要子类化 UIApplication。
main.m
的代码MyUIApplication.m
的代码:AppDelegate.m
的代码:在
- (BOOL)application:( UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
调用
[application beginReceivingRemoteControlEvents];
Can's answer was good, but I think it's outdated.
Now you need to subclass UIApplication.
code for
main.m
Code for
MyUIApplication.m
:Code for
AppDelegate.m
:inside of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
call
[application beginReceivingRemoteControlEvents];