我正在尝试在控制中心显示音频详细信息,如随附的屏幕截图所示。
使用以下代码,它在除 iOS11 之外的所有 iOS 版本中都按预期工作:
if ([MPNowPlayingInfoCenter class]) {
NSString *progress = (appDelegate.audioPlayer.progress == 0 || appDelegate.audioPlayer == nil ) ? @"0.0" : [NSString stringWithFormat:@"%f",appDelegate.audioPlayer.progress];
NSString *duration = (appDelegate.audioPlayer.progress == 0 || appDelegate.audioPlayer == nil ) ? @"0.0" :[NSString stringWithFormat:@"%f",appDelegate.audioPlayer.duration];
UIImage *image;
if (appDelegate.selectedImg.length == 0 || appDelegate.selectedImg == nil) {
image = [UIImage imageNamed:@"Music_Logo_1_"];
}
else{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.selectedImg]];
image = [UIImage imageWithData:data] != nil ? [UIImage imageWithData:data] : [UIImage imageNamed:@"Music_Logo_1_"];
}
NSDictionary *currentlyPlayingTrackInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:appDelegate.selectedTitle != nil && ![appDelegate.selectedTitle isEqualToString:@""] ? appDelegate.selectedTitle : @"", appDelegate.selecteddesc != nil && ![appDelegate.selecteddesc isEqualToString:@""] ? appDelegate.selecteddesc : @"", progress , duration,[[MPMediaItemArtwork alloc] initWithImage:image],@"Music", nil] forKeys:[NSArray arrayWithObjects:MPMediaItemPropertyTitle,MPMediaItemPropertyAlbumTitle, MPNowPlayingInfoPropertyElapsedPlaybackTime ,MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyArtwork,MPMediaItemPropertyArtist, nil]];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
}
MPNowPlayingInfoCenter
属性不适用于 iOS 11,它在较低版本的 iOS 中按预期工作。
iOS 11 中的输出:
预期结果: