iPhone SDK 检测耳机按钮点击

发布于 2024-12-05 00:29:43 字数 262 浏览 1 评论 0原文

有没有办法检测耳机的播放/暂停按钮点击?

我设法使用以下方法检测音量按钮点击:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-12-12 00:29:43

从应用程序外部完成的所有操作都被视为“远程事件”。如果您双击主页按钮并按此处的播放/暂停,则相当于按耳机上的播放/暂停按钮(双击下一个按钮,三次点击上一个按钮也是如此)。

这是有关事件处理的指南iOS 远程事件

就我个人而言,我喜欢子类化 MainWindow (UIWindow) 并重写 sendEvent: 方法,这样我就可以更直接地管理它:

- (void)sendEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl)
    {
        // Do stuff here
    }
    else
    {
        // Not my problem.
        [super sendEvent:event];
    }
}

希望有帮助,事件的枚举中央按钮是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 the sendEvent: method, so I can manage it more directly:

- (void)sendEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl)
    {
        // Do stuff here
    }
    else
    {
        // Not my problem.
        [super sendEvent:event];
    }
}

Hope that helps, the enum for the event of the central button is UIEventSubtypeRemoteControlTogglePlayPause.

习惯成性 2024-12-12 00:29:43

尝试了以上所有方法,但遗憾的是现在似乎都不起作用。
然后我看了一眼 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 中时,报告的事件是不同的。无论哪种方式,事件都会被检测到。

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

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.

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

Good thing is there is this example:

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.

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.

终陌 2024-12-12 00:29:43

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];

Can's answer was good, but I think it's outdated.

Now you need to subclass UIApplication.

code for 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]));
  }
}

Code for 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

Code for AppDelegate.m:

inside of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

call [application beginReceivingRemoteControlEvents];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文