我正在尝试检测连接到 iPhone 的耳机上的用户输入(点击)。到目前为止,我只发现了如何使用 AVAudioSession 检测中断。AVAudioSession 正确还是有其他方法?如何?
问问题
1157 次
2 回答
1
你要这个:
你在你的一个 VC 类中实现了一些东西:
// If using a nonmixable audio session category, as this app does, you must activate reception of
// remote-control events to allow reactivation of the audio session when running in the background.
// Also, to receive remote-control events, the app must be eligible to become the first responder.
- (void) viewDidAppear: (BOOL) animated {
[super viewDidAppear: animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
// Respond to remote control events
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playOrStop: nil];
break;
default:
break;
}
}
}
请参阅此处的示例代码。
于 2011-10-25T20:23:15.343 回答
1
现在更容易了,从 iOS 7 开始。要在按下耳机播放/暂停按钮时执行一个块:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"toggle button pressed");
return MPRemoteCommandHandlerStatusSuccess;
}];
或者,如果您更喜欢使用方法而不是块:
[commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];
停止:
[commandCenter.togglePlayPauseCommand removeTarget:self];
或者:
[commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];
您需要将其添加到文件的包含区域:
@import MediaPlayer;
于 2015-09-03T10:39:00.197 回答