我正在开发音乐播放器应用程序,当手机被锁定时,歌曲信息将显示在锁屏中,我在其中添加了以下方法
- (void)applicationWillResignActive:(UIApplication *)appdelegate的应用
-(void)setTalkPlayerTrackInfoForLockScreenPlayer
{
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter)
{
// Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
// if (playingInfoCenter){
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
NSString *albumArtImageURL = [[ShowDelegate sharedInstance].programImage objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];
NSURL *urls = [NSURL URLWithString:albumArtImageURL];
NSData *imageData = [NSData dataWithContentsOfURL:urls];
UIImage *imgToLockScreen;
if (imageData != nil) {
imgToLockScreen = [UIImage imageWithData:imageData];
}
else {
imgToLockScreen = [UIImage imageNamed:@"rlogo.png"];
}
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:imgToLockScreen];
NSNumber *duration = [NSNumber numberWithDouble:[[ShowDelegate sharedInstance].audioStreamer duration]];
NSNumber *currentTime = [NSNumber numberWithDouble:[[ShowDelegate sharedInstance].audioStreamer currentTime]];
NSNumber *playbackRate = [NSNumber numberWithDouble:1.0];
NSString *trackTitle = [[ShowDelegate sharedInstance].podtitle objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];
NSString *programName = [[ShowDelegate sharedInstance].programname objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];
NSString *trackTitle2 = [trackTitle stringByRemovingPercentEncoding];
NSString *sterg = [trackTitle2 stringByReplacingOccurrencesOfString:@"+" withString:@" "];
NSString *trackTitle1 = [programName stringByRemovingPercentEncoding];
NSString *sterg1 = [trackTitle1 stringByReplacingOccurrencesOfString:@"+" withString:@" "];
if (trackTitle != NULL) {
[songInfo setObject:sterg forKey:MPMediaItemPropertyTitle];
[songInfo setObject:sterg1 forKey:MPMediaItemPropertyArtist];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
[songInfo setObject:currentTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
}
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
}
现在歌曲信息显示在模拟器的锁定屏幕中,但在 os 10.2 的真实设备中不显示。我还添加了 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 如果有人知道我做错了什么,请帮忙?