iOS-Events[5]-Remote Control Events

iOS系统的Events类型分为以下几种:Multitouch events, Accelerometer events, Remote control events。

Events

Remote Control Events

Remote Control Events指通过外设的远程遥控或者通过手机的命令面板进行的控制事件,例如多媒体控制,音量调节,速率调节等。所有的Media Apps都应该支持该事件。

Handling Remote Control Events

接收远程的控制事件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"RemoteControl:Play");
...
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"RemoteControl:Pause");
...
return MPRemoteCommandHandlerStatusSuccess;
}];

注意,如果不设置Command的block事件,则对应的按钮不会在控制面板和锁屏页面出现。

Providing Now Playing Information

在控制面板和锁屏页面要能看到歌曲的信息,需要设置一下:

1
2
3
4
5
6
7
8
9
10
11
- (void)configureNowPlayingInfo
{
MPNowPlayingInfoCenter* info = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary* newInfo = [NSMutableDictionary dictionary];
[newInfo setObject:@"还没说出口的话" forKey:MPMediaItemPropertyTitle];
[newInfo setObject:@"唐琳" forKey:MPMediaItemPropertyArtist];
[newInfo setObject:[[MPMediaItemArtwork alloc]initWithImage:[UIImage imageNamed:@"Background.png"]] forKey:MPMediaItemPropertyArtwork];
[newInfo setObject:[NSNumber numberWithDouble:4 * 60 + 32] forKey:MPMediaItemPropertyPlaybackDuration];
[newInfo setObject:[NSNumber numberWithDouble:0] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
info.nowPlayingInfo = newInfo;
}