如何检测何时单击耳机中间按钮 [英] how to detect when middle headphone button is clicked

查看:50
本文介绍了如何检测何时单击耳机中间按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个单视图应用程序,在该应用程序中检测到原始iPhone耳机的中间按钮单击.

I want a single view application, where I detect a middle button click of my original iPhone headphone.

我尝试过

- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent
{
    if (theEvent.type == UIEventTypeRemoteControl) {
        switch(theEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                //Insert code

            case UIEventSubtypeRemoteControlPlay:
                //Insert code
                break;
            case UIEventSubtypeRemoteControlPause:
                // Insert code
                break;
            case UIEventSubtypeRemoteControlStop:
                //Insert code.
                break;
            default:
                return;
        }
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
 }

- (BOOL) canBecomeFirstResponder {
    return YES;
}

但是没有机会:(没有可捕获的事件.

But no chance :( There are no catchable events.

有人有主意吗?

推荐答案

上面已进行了所有尝试,但不幸的是现在似乎没有任何效果. 然后我偷看了beginReceivingRemoteControlEvents并找到了

Tried all above but sadly now none seems to work. Then I took a peek at beginReceivingRemoteControlEvents and found this

在iOS 7.1和更高版本中,使用共享的MPRemoteCommandCenter对象注册远程控制事件.使用共享的命令中心对象时,无需调用此方法.

In iOS 7.1 and later, use the shared MPRemoteCommandCenter object to register for remote control events. You do not need to call this method when using the shared command center object.

然后签出MPRemoteCommandCenter,最后进入MPRemoteCommand文档页面.

Then checked out MPRemoteCommandCenter and finally ended up in MPRemoteCommand documentation page.

好东西是这个例子:

let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget(handler: { (event) in    

    // Begin playing the current track    
    self.myMusicPlayer.play()
    return MPRemoteCommandHandlerStatus.success
})

现在,如果我们想阅读中间按钮,我们可以做:

Now if we want to read middle button we can do:

MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in

 // middle button (toggle/pause) is clicked
 print("event:", event.command)

 return .success
}

这行得通,我设法检测到了耳机的中键.

This works and I managed to get the headphone's middle button detected.

注意: 我注意到有不同的行为,具体取决于我们在上面放置此类代码的位置. 就是说,当我将其放入View Controller中时,所报告的事件是相同的,而当我将其放入AppDelegate的didFinishLaunching中时,所报告的事件将有所不同.无论哪种方式都可以检测到事件.

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.

这篇关于如何检测何时单击耳机中间按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆