1

I am trying to detect if user press the headphone keys for that i am using 2 methods.

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

But after calling this method i did't get ,whats wrong in my code and i enabled background service for audio check & i am using this all methods in NSObject class.

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

2 Answers2

0

Please check the below code.

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

Try including this in viewDidAppear

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

And this in viewDidDisappear

[[AVAudioSession sharedInstance] setActive:NO error:nil];
Dombi Bence
  • 746
  • 7
  • 23