如何在iOS-8的Objective-C中使用控制中心上的图像设置音频细节?
我正在尝试在控制中心上显示音频详细信息,如附带的屏幕截图所示。
使用以下代码,除了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中的输出:
预期结果: