2016-07-22 101 views
0

我想檢測用戶是否按下了耳機鍵,因爲我使用了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; 
     } 
    } 
} 

但調用此方法餘did't得到的,什麼是錯在我的代碼和我啓用後臺服務,爲音頻檢查&我使用這NSObject類的所有方法之後。

+0

您是否缺少:在您的選擇器中爲MPRemoteCommandCenter定義選擇器時,因爲該方法需要參數? – ldindu

+0

@ldindu不,我沒有錯過 –

回答

0

請檢查以下代碼。

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

但我想檢查的是用戶按下任何按鈕音量,播放/暫停感謝帖子:) –