锁屏状态下显示专辑图片、信息、进度等等
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
UIImage *image = [UIImage imageNamed:@"image"];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:image];
//歌曲名称
[songInfo setObject:@"深夜地下铁" forKey:MPMediaItemPropertyTitle];
//演唱者
[songInfo setObject:@"陶钰玉" forKey:MPMediaItemPropertyArtist];
//专辑名
[songInfo setObject:@"深夜地下铁" forKey:MPMediaItemPropertyAlbumTitle];
//专辑缩略图
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[songInfo setObject:[NSNumber numberWithDouble:[audioY getCurrentAudioTime]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; //音乐当前已经播放时间
[songInfo setObject:[NSNumber numberWithFloat:1.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];//进度光标的速度 (这个随 自己的播放速率调整,我默认是原速播放)
[songInfo setObject:[NSNumber numberWithDouble:[audioY getAudioDuration]] forKey:MPMediaItemPropertyPlaybackDuration];//歌曲总时间设置
// 设置锁屏状态下屏幕显示音乐信息
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
过程中遇到一些问题
1.只添加MPMediaItemPropertyPlaybackDuration(歌曲总时间)
问题:不显示当前进度
解决:添加MPMediaItemPropertyPlaybackDuration同时也出现问题,剩余时间每次减2秒
2.添加了MPMediaItemPropertyPlaybackDuration和MPNowPlayingInfoPropertyElapsedPlaybackTime
问题:出现了剩余时间每次减2秒的情况
解决:要添加MPNowPlayingInfoPropertyPlaybackRate就可以
3.暂停恢复播放更新 锁屏进度条
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]];
[dict setObject:[NSNumber numberWithDouble:audioPlayer.playableDuration] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; //音乐当前已经过时间
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];