锁屏状态下,看到歌手图片

第一步,检查你的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];
}
}
上面的if (NSClassFromString(@"MPNowPlayingInfoCenter"))避免了版本兼容问题,这个API貌似只出现在5里面。


现在你可以发现,上面的代码可能不起作用,而且待机屏的播放暂停也不会起作用。
第三步,在appDelegate的加载完成回调里面写
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
你会发现,,只有信息出来,但是播放控制还是不起作用。。。。。

呵呵,绕弯子了。


重写这个方法了
- (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,炫炫的效果出来了,,,,,
当然becomeFirstResponder也可以写在ViewControll或者View里面。
但是作为整个应用的状态来说,个人认为,写在appDelegate比较好。

这里有点考虑,becomeFirstResponder后,会不会对应用的某些功能有影响,,,


最后附MPNowPlayingInfoCenter可显示的内容,但是本人还有些没有试出来,,不知为何。。。

some properties specific to this class. In iOS 5.0, the now playing info center supports the following media item property keys:

MPMediaItemPropertyAlbumTitle
MPMediaItemPropertyAlbumTrackCount
MPMediaItemPropertyAlbumTrackNumber
MPMediaItemPropertyArtist
MPMediaItemPropertyArtwork
MPMediaItemPropertyComposer
MPMediaItemPropertyDiscCount
MPMediaItemPropertyDiscNumber
MPMediaItemPropertyGenre
MPMediaItemPropertyPersistentID
MPMediaItemPropertyPlaybackDuration
MPMediaItemPropertyTitle


Additional Metadata Properties
These properties for the now playing info center dictionary supplement the available media item properties, as described in the Overview in this document.

NSString *const MPNowPlayingInfoPropertyElapsedPlaybackTime
NSString *const MPNowPlayingInfoPropertyPlaybackRate;
NSString *const MPNowPlayingInfoPropertyPlaybackQueueIndex;
NSString *const MPNowPlayingInfoPropertyPlaybackQueueCount;
NSString *const MPNowPlayingInfoPropertyChapterNumber;
NSString *const MPNowPlayingInfoPropertyChapterCount;


http://tieba.baidu.com/p/1394006679

你可能感兴趣的:(iphone开发)