【发布时间】:2011-03-09 06:23:31
【问题描述】:
我想让我的应用在多任务处理时使用锁定屏幕上的音频按钮。 (是的,就像潘多拉一样。) 我希望使用什么 API?
【问题讨论】:
标签: iphone multitasking
我想让我的应用在多任务处理时使用锁定屏幕上的音频按钮。 (是的,就像潘多拉一样。) 我希望使用什么 API?
【问题讨论】:
标签: iphone multitasking
请参阅Remote Control of Multimedia 文档。基本上,您只需要在共享应用程序实例上调用-beginReceivingRemoteControlEvents,然后将某些东西(可能是您的主视图控制器)注册为第一响应者并在其上实现-remoteControlReceivedWithEvent: 方法。您将从锁屏控件和耳机答题器以及多任务抽屉左侧的控制按钮中获取事件。要在您的应用程序不是最重要的时候播放音频,您还应该在后台音频上查看this information。
【讨论】:
现在更容易了,从 iOS 7 开始。这是播放/暂停切换(耳机按钮)的示例。有关更多选项,请参阅 MPRemoteCommandCenter 和 MPRemoteCommand 的文档。
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;
【讨论】: