标签:
在做Iphone多媒体开发的时候都会用到的捕获系统远端事件:remoteControlReceivedWithEvent,查看api就知道需要在我们的ui准备好的时候注册接收RemoteControlEvents,一般是重写主界面的Controller里面viewDidAppear和和canBecomeFirstResponder方法,如下:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIDevice *device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ( [device respondsToSelector:@selector(isMultitaskingSupported)] )
{
backgroundSupported = device.multitaskingSupported;
}
if ( backgroundSupported == YES )
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//注意这里,告诉系统已经准备好了
[self becomeFirstResponder];
}
}
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
case UIEventSubtypeRemoteControlStop:
{
//todo stop event
break;
}
case UIEventSubtypeRemoteControlNextTrack:
{
//todo play next song
break;
}
case UIEventSubtypeRemoteControlPreviousTrack:
{
//todo play previous song
break;
}
default:
break;
}
}
}
经过上面的代码,在正常情况下也没有什么问题,但是要是你的成功中有调用了resignFirstResponder,之后你会发现就捕获不到 remoteEvent了,原因是系统的FirstResponder时间控制是一个全局的,所以调用了resignFirstResponder就会影 响到becomeFristResponder,解决办法就是在用resignFirstResponder后面再调用一下 becomeFirstResponder接口就好了。
上面的笔记是我自己在做ipad音乐项目的经验,这个bug曾经让我找了两天,今天在这里分享出来,希望同行的兄弟少走弯路。
转载自: http://blog.sina.com.cn/s/blog_468eb48e01011c6n.html
标签:
原文地址:http://www.cnblogs.com/Cheetah-yang/p/4656235.html