Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检测耳机按键?_Ios_Objective C_Iphone_Remote Access_Headphones - Fatal编程技术网

Ios 如何检测耳机按键?

Ios 如何检测耳机按键?,ios,objective-c,iphone,remote-access,headphones,Ios,Objective C,Iphone,Remote Access,Headphones,我试图检测用户是否按下耳机键,因为我使用了两种方法 -(void)headsetMicrophoneDetection { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(onToogleP

我试图检测用户是否按下耳机键,因为我使用了两种方法

-(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类中使用此所有方法。

请检查以下代码

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

请检查下面的代码

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

尝试将其包含在
viewDidAppear

[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
而这在
view中消失了

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

尝试将其包含在
viewDidAppear

[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
而这在
view中消失了

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

但我想检查用户是否按了任何按钮音量,播放/暂停谢谢帖子:)但我想检查用户是否按了任何按钮音量,播放/暂停感谢您的帖子:)当为MPRemoteCommandCenter定义选择器时,您是否缺少:在选择器中,因为该方法需要参数?@ldindu不,我没有,di也会感到奇怪。从那时起,您找到了解决方案吗?以后没有客户端跳过此选项@GyörgyBenedekAre您缺少:在您的选择器中定义MPRemoteCommandCenter的选择器时,该方法需要一个参数?@ldindu不,我没有错过。我也会感到奇怪。从那以后你找到解决方案了吗?以后没有客户跳过这个选项@GyörgyBenedek