如何检测耳机按键?
问题描述:
我想检测用户是否按下了耳机键,因为我使用了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类的所有方法之后。
答
请检查以下代码。
- (BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return YES;
}
return NO;}
+0
但我想检查的是用户按下任何按钮音量,播放/暂停感谢帖子:) –
您是否缺少:在您的选择器中为MPRemoteCommandCenter定义选择器时,因为该方法需要参数? – ldindu
@ldindu不,我没有错过 –