- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 开启后台任务
// 会在不确定的时间被关闭 用呼吸管理者,来开始进入后台任务 ,可以提高进入后台的优先级
[application beginBackgroundTaskWithExpirationHandler:nil];
}
3.2为了提高后台的优先级
在info.plist添加后台的任务的模式, 为音乐播放
Required background modes
item App plays audio or streams audio/video using AirPlay
3.3 为了在跟别的音乐进入后台时会产生混音,得注册会话模型,有混响的,录音的,其他音乐停止,自己的音乐播放
// 注册会话模式
// 创建一个会话模式
AVAudioSession *session = [AVAudioSession sharedInstance];
// http://blog.youkuaiyun.com/daiyelang/article/details/16986059
//设置音乐播放器的会话类型 ,当你播放的时候,其他音乐程序停止
[session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
// 注册会话
[session setActive:YES error:nil];
4.音乐播放器被打断,打断结束后继续播放, 如打电话
#pragma mark --AVAudioPlayer的代理
// 音乐播播放在开始的时候被打断
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
NSLog(@"被打断");
}
// 音乐播放在结束时被调用
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
NSLog(@"结束打断");
// 开始继续播放
[player play];
}
5.音乐播放器,进入锁屏界面的出画面和信息
/**
* 设置锁屏界面
*/
- (void)setupLockScreen:(XJMusic *)music
{
//1.创建一个信息中心
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
// 2.初始化化信息中心数据
NSMutableDictionary *md = [NSMutableDictionary dictionary];
// 标题
md[MPMediaItemPropertyTitle] = music.name;
// 专辑
md[MPMediaItemPropertyAlbumTitle] = music.singer;
// 作者
md[MPMediaItemPropertyArtist] = music.singer;
// 封面图片
md[MPMediaItemPropertyArtwork] =[[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:music.icon]];
// 3.更新信息中心的数据
center.nowPlayingInfo = md;
// 4.监听远程事件(播放事件)
// 开始播放
#warning 注意, 一定要写下面的代码才能在锁屏界面看到信息
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
6.设置锁屏界面的歌词:思路是根据播放的时间,去取歌词绘制一张图片,让后代替掉,无限循环这样
重点:实时监听播放的时间
解决办法:开启定时器,一首一个定时器
不断的调用AVAudioPlayer的currentTime方法获取当 前播放的进度
- (CADisplayLink *)link
{
if (_link == nil) {
_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updata)];
}
return _link;
}
- (void)updata
{
// 不断的调用AVAudioPlayer的currentTime方法获取当前播放的进度
// 频率是 1/60
NSLog(@"%.f %.f", self.audioPlayer.duration , self.audioPlayer.currentTime);
}
在选中某首歌时开启定时器
[self.link invalidate]; // 关闭上一次定时器
self.link = nil;
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; // 开启定时器
// 7.快进
// 允许快进
audioPlayer.enableRate = YES;
//播放速度
audioPlayer.rate = 50.0;
8.一首歌播放完后自动跳跳到下一首歌
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
// NSLog(@"播放完毕");
// 自动播放下一首
// 动态获取当前正在播放的行
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSInteger currnetRow = path.row;
// 计算下一行的值
NSInteger nextRow = currnetRow + 1;
if (nextRow >= self.musics.count) {
nextRow = 0;
}
#warning 停止上一首音乐的动画
// 取出当前播放的模型
NJMusic *music = self.musics[path.row];
music.playing = NO;
// 取出当前播放行的cell
CZMusicViewCell *cell = (CZMusicViewCell *)[self.tableView cellForRowAtIndexPath:path];
cell.music = music;
// 播放下一首
NSIndexPath *nextPath = [NSIndexPath indexPathForItem:nextRow inSection:0];
[self tableView:nil didSelectRowAtIndexPath:nextPath];
// 主动选中下一行
[self.tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
9.图层动画进入后台会不转,所以不选图层动画用transform