1、后台播放
2、锁屏界面展示,这个要真机上才可以
- iOS模拟器中播放音乐退到后台还是有用的,但是真机不行,要做如下两步操作
1、设置模式
2、开启后台会话,设置后台播放的类别
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 获取音频会话
AVAudioSession *session = [AVAudioSession sharedInstance];
// 设置后台播放类型
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
// 激活会话
[session setActive:YES error:nil];
return YES;
}
- 锁屏歌词显示
需要导入MediaPlayer/MediaPlayer.h
#pragma mark - 生成锁屏界面的图片
- (void)generatorLockImage {
// 拿到当前播放歌曲的图片
MusicModel *musicData = self.musicData[self.index];
NSURL *url = [NSURL URLWithString:musicData.picUrl];
UIImage *currentImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
NSLog(@"%@", musicData.name);
NSLog(@"%@", currentImage);
// 拿到三句歌词
LXYLrc *currentLrc = self.lyrices[self.currentIndex];
NSInteger previousIndex = self.currentIndex - 1;
LXYLrc *previousLrc = nil;
if (previousIndex >= 0) {
previousLrc = self.lyrices[previousIndex];
}
NSInteger nextIndex = self.currentIndex + 1;
LXYLrc *nextLrc = nil;
if (nextIndex < self.lyrices.count) {
nextLrc = self.lyrices[nextIndex];
}
// 生成水印图片
// 图形上下文
UIGraphicsBeginImageContext(currentImage.size);
// 画图片
[currentImage drawInRect:CGRectMake(0, 0, currentImage.size.width, currentImage.size.height)];
// 画文字
CGFloat titleH = 25;
// 设置文字居中
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
NSDictionary *attributesDic1 = @{
NSFontAttributeName : [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName : [UIColor lightGrayColor],
NSParagraphStyleAttributeName : style
};
[previousLrc.content drawInRect:CGRectMake(0, currentImage.size.height - 3 * titleH, currentImage.size.width, titleH) withAttributes:attributesDic1];
[nextLrc.content drawInRect:CGRectMake(0, currentImage.size.height - titleH, currentImage.size.width, titleH) withAttributes:attributesDic1];
NSDictionary *attributesDic2 = @{
NSFontAttributeName : [UIFont systemFontOfSize:17],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSParagraphStyleAttributeName : style
};
[currentLrc.content drawInRect:CGRectMake(0, currentImage.size.height - 2 * titleH, currentImage.size.width, titleH) withAttributes:attributesDic2];
// 生成图片
UIImage *lockImage = UIGraphicsGetImageFromCurrentImageContext();
[self setupLockScreenInfoWithLockImage:lockImage];
}
#pragma mark - 设置锁屏界面的信息
- (void)setupLockScreenInfoWithLockImage:(UIImage *)lockImage {
// 获取当前正在播放的歌曲
MusicModel *music = self.musicData[_index];
// 获取锁屏界面中心
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
// 设置展示的信息
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionary];
[playingInfo setObject:music.name forKey:MPMediaItemPropertyAlbumTitle]; // 歌名
[playingInfo setObject:music.singer forKey:MPMediaItemPropertyArtist]; // 歌手
MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithImage:lockImage];
[playingInfo setObject:artWork forKey:MPMediaItemPropertyArtwork]; // 歌词居中
[playingInfo setObject:@(self.duration) forKey:MPMediaItemPropertyPlaybackDuration]; // 歌曲总时间
[playingInfo setObject:@(self.currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; // 当前时间
playingInfoCenter.nowPlayingInfo = playingInfo;
// 可以让应用程序接受远程事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
网友评论