转自 http://tieba.baidu.com/p/1394006679
第一步,检查你的AppDelegate是不是继承于UIResponder,有的是自动生成,有的人习惯手写,如果是继承于NSObject请改为 AppDelegate : UIResponder
第二步,在音乐播放的时候,写上歌曲信息和图片信息:
- (void) setMediaInfo : (UIImage *) img andTitle : (NSString *) title andArtist : (NSString *) artist
{
MYDEBUGPRINT(NSLog(@"begen set album art, to MPNowPlayingInfoCenter."));
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[dict setObject:title forKey:MPMediaItemPropertyAlbumTitle];
[dict setObject:artist forKey:MPMediaItemPropertyArtist];
[dict setObject:[NSNumber numberWithInt:[playlist.refresh intValue]] forKey:MPMediaItemPropertyPlaybackDuration];
MPMediaItemArtwork * mArt = [[MPMediaItemArtwork alloc] initWithImage:img];
[dict setObject:mArt forKey:MPMediaItemPropertyArtwork];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
[dict release];
}
}
- (void) setMediaInfo : (UIImage *) img andTitle : (NSString *) title andArtist : (NSString *) artist
{
MYDEBUGPRINT(NSLog(@"begen set album art, to MPNowPlayingInfoCenter."));
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[dict setObject:title forKey:MPMediaItemPropertyAlbumTitle];
[dict setObject:artist forKey:MPMediaItemPropertyArtist];
[dict setObject:[NSNumber numberWithInt:[playlist.refresh intValue]] forKey:MPMediaItemPropertyPlaybackDuration];
MPMediaItemArtwork * mArt = [[MPMediaItemArtwork alloc] initWithImage:img];
[dict setObject:mArt forKey:MPMediaItemPropertyArtwork];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
[dict release];
}
}
上面的if (NSClassFromString(@"MPNowPlayingInfoCenter"))避免了版本兼容问题,这个API貌似只出现在5里面。
现在你可以发现,上面的代码可能不起作用,而且待机屏的播放暂停也不会起作用。
现在你可以发现,上面的代码可能不起作用,而且待机屏的播放暂停也不会起作用。
第三步,在appDelegate的加载完成回调里面写
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
你会发现,,只有信息出来,但是播放控制还是不起作用。。。。。 重写这个方法了
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
最后一步,加上控制逻辑的监听:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"xxxxxxxxxxx11111");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"xxxxxxxxxxx3333");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"xxxxxxxxxxx4444");
break;
default:
break;
}
}
}
OK,炫炫的效果出来了,,,,,
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"xxxxxxxxxxx11111");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"xxxxxxxxxxx3333");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"xxxxxxxxxxx4444");
break;
default:
break;
}
}
}
OK,炫炫的效果出来了,,,,,
当然becomeFirstResponder也可以写在ViewControll或者View里面。
但是作为整个应用的状态来说,个人认为,写在appDelegate比较好。
这里有点考虑,becomeFirstResponder后,会不会对应用的某些功能有影响,,,
但是作为整个应用的状态来说,个人认为,写在appDelegate比较好。
这里有点考虑,becomeFirstResponder后,会不会对应用的某些功能有影响,,,
http://blog.sina.com.cn/s/blog_71715bf801019xxr.html 比较系统的讲解