如果正在播放歌曲并且用户锁定了他的iPhone,我想在锁定屏幕上显示播放(搜索栏,下一个按钮和前置按钮)和歌曲名称,艺术家姓名,图像等。但我有一个问题。问题是MPNowPlayingInfoCenter信息值显示1-2秒,之后所有值将自动消失,之后显示默认值。
这下面的截图1是完美的但是在1-2之后这个所有值都消失了,看到截图2所有值都消失了
-(void)SetLockScreenView :(NSString *)SongName ArtistName:(NSString *)ArtistName AlbumTitle:(NSString *)AlbumTitle DisplayImg:(NSString *)displayImg
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
if(displayImg.length>0 && displayImg!=nil)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:displayImg]];
dispatch_async(dispatch_get_main_queue(), ^{
if(data!=nil)
{
UIImage *image = [UIImage imageWithData:data];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: image];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
}
else
{
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"artistDefaultImg"]];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
}
[songInfo setObject:SongName forKey:MPMediaItemPropertyTitle];
[songInfo setObject:ArtistName forKey:MPMediaItemPropertyArtist];
[songInfo setObject:AlbumTitle forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:[NSNumber numberWithFloat:self.manager.currentItem.duration] forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:[NSNumber numberWithInt:0] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
NSLog(@"songInfo :%@",songInfo);
});
});
}
}
}
#pragma mark - Remote Control
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.manager.player play];
break;
case UIEventSubtypeRemoteControlPause:
[self.manager.player pause];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
if ([self.manager.player isPlaying]) {
[self.manager.player pause];
}
else {
[self.manager.player play];
}
break;
case UIEventSubtypeRemoteControlNextTrack:
[self Onclick_next:self];
NSLog(@"Next song play");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self Onclick_prev:self];
NSLog(@"Prev song play");
break;
default:
break;
}
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}