后台相关

作者: Mr丶炎 | 来源:发表于2016-08-11 17:05 被阅读21次

1、后台播放
2、锁屏界面展示,这个要真机上才可以

  • iOS模拟器中播放音乐退到后台还是有用的,但是真机不行,要做如下两步操作
    1、设置模式
Snip20160811_2.png

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];
    
}

相关文章

  • 后台相关

    1、后台播放2、锁屏界面展示,这个要真机上才可以 iOS模拟器中播放音乐退到后台还是有用的,但是真机不行,要做如下...

  • iOS 后台相关

    在iOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态。在这种状态下,应用程序不执行任何...

  • 搭建 后台相关

    首先下载 jenkins war包http://mirrors.jenkins.io/war/latest/[ht...

  • iOS与PHP通讯协议设计-----3(用户注册验证)

    用户注册的后台相关操作示例:

  • docker操作命令记录

    "docker 常用命令" 仓库相关 容器相关 镜像相关 run命令相关 -d,后台运行容器, 并返回容器ID;不...

  • 后台运行jar包

    1、后台运行jar包方法: nohup意为后台不挂断运行,与是否账号退出无关 2、后台运行,并将相关日志输出到sp...

  • iOS 后台推送 收不到

    iOS 前台可以收到推送,后台死活收不到推送。 上述方法再后台死活不执行。参考极光官网后台没有配置相关参数cont...

  • 常用Ubuntu指令

    进程相关 查看当前终端后台运行的任务 jobs 查看所有进程 ps -ef 杀死进程 kill 进程pid 后台运...

  • Spring与Mybatis整合后的SQL异常捕获

    优秀文章 博客:永生天地 > Spring的DataAccessException 相关框架 后台:Spring,...

  • 【转载】接口文档

    康保健康后台标签(java后台): java 一、用户登陆相关1,注册[POST] rest/hUserContr...

网友评论

    本文标题:后台相关

    本文链接:https://www.haomeiwen.com/subject/lprssttx.html