原地址:http://geagle001.blog.163.com/blog/static/188226090201173110404762/
用户要实现后台的播放的控制,需要按下面的方法来实现。这样实现的效果就是和iPod后台播放的效果一样。在后台播放音乐的时候可以通过任务栏来控制。任务栏通过双击Home键来调出,调出后向左滑到最前门可以看到这个控制按钮。如下图:
1.在View显示的时候接收remote-control事件
- (void)viewDidAppear:(BOOL)animated {
|
[super viewDidAppear:animated];
|
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
|
[self becomeFirstResponder];
|
}
|
2.在View消失或者我们不需要响应控制的时候取消接收remote-control 事件
- (void)viewWillDisappear:(BOOL)animated {
|
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
|
[self resignFirstResponder];
|
[super viewWillDisappear:animated];
|
}
|
3 重新下面的函数
- (BOOL) canBecomeFirstResponder {
|
return YES;
|
}
|
4 接收遥控事件
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
|
|
if (receivedEvent.type == UIEventTypeRemoteControl) {
|
|
switch (receivedEvent.subtype) {
|
|
case UIEventSubtypeRemoteControlTogglePlayPause:
|
[self playOrStop: nil];
|
break;
|
|
case UIEventSubtypeRemoteControlPreviousTrack:
|
[self previousTrack: nil];
|
break;
|
|
case UIEventSubtypeRemoteControlNextTrack:
|
[self nextTrack: nil];
|
break;
|
|
default:
|
break;
|
}
|
}
|
}
|