【问题标题】:how to detect when middle headphone button is clicked如何检测何时单击中间耳机按钮
【发布时间】:2015-06-01 13:00:31
【问题描述】:

我想要一个单视图应用程序,我可以在其中检测到我原来的 iPhone 耳机的中间按钮点击。

我试过了

- (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;
}

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

有人有想法吗?

【问题讨论】:

标签: ios iphone


【解决方案1】:

以上都试过了,但遗憾的是现在似乎都没有。 然后我看了一下beginReceivingRemoteControlEvents,发现了这个

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

然后查看MPRemoteCommandCenter,最后进入MPRemoteCommand文档页面。

好在有这个例子:

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

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

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

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

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

 return .success
}

这很有效,我设法检测到耳机的中间按钮。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2019-12-23
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多