【问题标题】:How to detect the headphone key press?如何检测耳机按键?
【发布时间】:2016-11-26 00:29:06
【问题描述】:

我正在尝试检测用户是否按下耳机键,因为我正在使用 2 种方法。

-(void)headsetMicrophoneDetection
{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(onTooglePlayPause)];

    NSLog(@"calling headset method");
}
-(void)onTooglePlayPause
{
    NSLog(@"kishore");
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent
{
    NSLog(@"callig method to :)");
    if (theEvent.type == UIEventTypeRemoteControl) {
        switch(theEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"Hello");
                break;
            case UIEventSubtypeRemoteControlPlay:
                NSLog(@"Hello 2");
                break;
            case UIEventSubtypeRemoteControlPause:
                NSLog(@"Hello 3");
                break;
            case UIEventSubtypeRemoteControlStop:
                NSLog(@"Hello 4");
                break;
            default:
                return;
        }
    }
}

但是在调用这个方法后我没有得到,我的代码有什么问题,我启用了后台服务进行音频检查,我在 NSObject 类中使用了所有方法。

【问题讨论】:

  • 在为 MPRemoteCommandCenter 定义选择器时是否缺少:在选择器中,因为方法需要一个参数?
  • @ldindu 我没有错过
  • 我也想知道。从那以后你找到解决方案了吗?
  • 以后没有客户跳过这个选项@GyörgyBenedek

标签: ios objective-c iphone remote-access headphones


【解决方案1】:

请检查以下代码。

- (BOOL)isHeadsetPluggedIn {
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
        if ([[desc portType]     isEqualToString:AVAudioSessionPortHeadphones])
           return YES;
    }
    return NO;}

【讨论】:

  • 但我想检查用户是否按下了任何按钮音量,播放/暂停感谢您的帖子:)
【解决方案2】:

尝试在 viewDidAppear 中包含此内容

[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

这个在viewDidDisappear

[[AVAudioSession sharedInstance] setActive:NO error:nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多