为了账号安全,请及时绑定邮箱和手机立即绑定

iOS AVPlayer 实现后台连续播放视频

标签:
iOS

最近接到一个需求,需要做一个在后台播放视频的功能。折腾了一下,最后总算完成了。因此写一篇文章,介绍下具体的实现步骤,也说说自己遇到的坑,算是总结和记录。

前言

当 App 退到后台时,会进入 suspend 状态,若此时在播放视频,则会自动暂停。我们需要实现的效果是,当 App 退到后台时,视频中的声音还能继续播放。另外,我们还同时实现视频的连续播放功能,和在锁屏界面控制视频播放的功能。具体怎么做,下面听我一一道来。

注意:由于 iOS 模拟器存在 BUG,尤其是 iOS 11 的模拟器,不能在后台播放音频,因此以下功能最好使用真机测试。

一、后台播放音频

要实现后台播放视频功能,首先需要实现后台播放音频功能。实现后台播放音频很简单,只要简单配置一下就可以了。总共有三步:

1. 修改 Info.plist

Info.plist 中添加 Required background modes ,并在下面添加一项 App plays audio or streams audio/video using AirPlay 。如图所示:

https://img1.sycdn.imooc.com//5d53ff850001cf6a08880207.jpg

2. 修改 Capabilities

Capabilities 中开启 Background Modes 。如图所示:

https://img1.sycdn.imooc.com//5d53ff880001af2508810343.jpg

3. 修改 AppDelegate

AppDelegateapplication: didFinishLaunchingWithOptions: 方法中,添加以下代码:

// 告诉app支持后台播放AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];

至此就实现了后台播放音频的功能,但这不是我们的最终目的,请继续往下看。

二、后台播放视频

网上讲实现后台播放视频的资料并不多(可能比较少有这么坑的需求)。我在网上找了一圈,只有 这篇文章 提到了,方法也很简单,分为两步:

1. 退到后台时移除 playerLayer 上的 player

viewController 中添加退到后台监听:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(removePlayerOnPlayerLayer)
               name:UIApplicationDidEnterBackgroundNotification
             object:nil];

移除 player :

- (void)removePlayerOnPlayerLayer {
    
    _playerLayer.player = nil;
}

2. 回到前台时重新添加 player

viewController 中添加回到前台监听:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(resetPlayerToPlayerLayer)
               name:UIApplicationWillEnterForegroundNotification
             object:nil];

重新添加 player :

- (void)resetPlayerToPlayerLayer {
    
    _playerLayer.player = _player;
}

这样简单的后台播放视频就实现了。

对于上面的实现后台播放视频的方法,我的理解是,iOS 是支持后台播放音频的,而 AVPlayer 在播放视频时,会将图像渲染在 layer 上,因此只要取消图像的渲染,只播放音频,就可以实现后台播放。

3. 连续播放视频

后台连续播放视频的逻辑,其实和前台连续播放的逻辑一样。可以通过监听 playerItem 播放结束的通知来切换歌曲,则当播放结束时,需要移除对当前 playerItem 的监听,然后添加下一个 playerItem 的监听。

这里直接通过判断进度条是否完成,来切换歌曲。

// 监听播放进度__weak ViewController * weakSelf = self;
[self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1, NSEC_PER_SEC)
                                          queue:NULL
                                     usingBlock:^(CMTime time) {
                                         
                                         [weakSelf updateProgressView];
                                     }];
// 更新进度条进度- (void)updateProgressView {    
    self.currentDuration = CMTimeGetSeconds(_player.currentItem.duration);    
    CGFloat progress = CMTimeGetSeconds(_player.currentItem.currentTime) / _currentDuration;    
    if (progress == 1.0f) {
        [self playNextVideo];  // 播放下一个视频
    } else {
        [_viewVideoProgress setValue:progress];   // 更新进度条
    }
}

下面插播一条 CMTime 的广告。可跳过。

上面监听播放进度的时候,用到了一个叫 CMTime 的东西,这里简单地讲一下我的理解。
一般我们用 CMTime 的时候,都是使用 CMTimeGetSeconds(time) 将它转成秒数。
那为何不直接使用 NSTimeInterval 来表示时间就好了?

原因只有一个 —— 精度

浮点数没有办法进行准确的加减运算,当多次加减后,可能会出现较大误差。因此在视频一般用 CMTime 来表示时间,因为 CMTime 可以规定最小的精度,从而保证累加后时间的准确性。

CMTime 的构造方法 CMTimeMakeWithSeconds(seconds, timescale)seconds 表示秒数, 1 / timescale 表示最小精度。
另一个构造方法 CMTimeMake(value, timescale) ,其中 seconds = value / timescale
CMTimeMakeWithSeconds(1, 1000) 等价于 CMTimeMake(1000, 1000) ,都表示 1 秒,最小精度为 0.001 。

注意:需要满足 seconds >=  1 / timescale ,即 value > 1,这也是精度存在的意义。

三、添加远程控制

1. 用 MPNowPlayingInfoCenter 显示歌曲信息

先上代码:

// 更新锁屏界面信息- (void)updateLockScreenInfo {    
    if (!_player) {        return;
    }    
    // 1.获取锁屏中心
    MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];    // 初始化一个存放音乐信息的字典
    NSMutableDictionary *playingInfoDict = [NSMutableDictionary dictionary];    
    // 2、设置歌曲名
    [playingInfoDict setObject:[NSString stringWithFormat:@"歌曲%ld", (long)_currentIndex + 1]
                        forKey:MPMediaItemPropertyTitle];
    [playingInfoDict setObject:[NSString stringWithFormat:@"专辑%ld", (long)_currentIndex + 1]
                        forKey:MPMediaItemPropertyAlbumTitle];    
    
    // 3、设置封面的图片
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"cover%ld.jpg", (long)_currentIndex + 1]];    if (image) {        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
        [playingInfoDict setObject:artwork forKey:MPMediaItemPropertyArtwork];
    }    
    // 4、设置歌曲的时长和已经消耗的时间
    NSNumber *playbackDuration = @(CMTimeGetSeconds(_player.currentItem.duration));    NSNumber *elapsedPlaybackTime = @(CMTimeGetSeconds(_player.currentItem.currentTime));    if (!playbackDuration || !elapsedPlaybackTime) {        return;
    }
    [playingInfoDict setObject:playbackDuration
                        forKey:MPMediaItemPropertyPlaybackDuration];
    [playingInfoDict setObject:elapsedPlaybackTime
                        forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [playingInfoDict setObject:@(_player.rate) forKey:MPNowPlayingInfoPropertyPlaybackRate];    
    //音乐信息赋值给获取锁屏中心的nowPlayingInfo属性
    playingInfoCenter.nowPlayingInfo = playingInfoDict;
}

注意: updateLockScreenInfo 不需要频繁调用,锁屏界面的进度条会自己计时,只需要在关键的时刻去同步这个已播放时长。一般需要调用的时刻有,切换歌曲、暂停、播放、拖动进度条等。

这里有个坑。我们知道 player 有个 rate 属性,为 0 的时候表示暂停,为 1.0 的时候表示播放。相应的, nowPlayingInfo 也有个 MPNowPlayingInfoPropertyPlaybackRate 属性。前面说到,「锁屏界面的进度条会自己计时」,它是否在计时就是取决于这个属性。坑的地方在于,这个属性和 playerrate 并不同步。也就是说,单纯地在锁屏界面点暂停后, player 会暂停, rate 也会变成 0 ,但是 MPNowPlayingInfoPropertyPlaybackRate 却不为 0 。导致的结果是,在锁屏界面点击了暂停按钮,这个时候进度条表面看起来停止了走动,但是其实还是在计时,所以再点击播放的时候,锁屏界面进度条的光标会发生位置闪动。

解决方法:在视频暂停和播放的时候,同步视频的已播放时长 _player.currentItem.currentTimeMPNowPlayingInfoPropertyElapsedPlaybackTime 、视频的当前播放速率  _player.rateMPNowPlayingInfoPropertyPlaybackRate

2. 用 MPRemoteCommandCenter 实现播放控制

先上代码:

// 添加远程控制- (void)createRemoteCommandCenter {    
    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];    
    MPRemoteCommand *pauseCommand = [commandCenter pauseCommand];
    [pauseCommand setEnabled:YES];
    [pauseCommand addTarget:self action:@selector(remotePauseEvent)];    
    MPRemoteCommand *playCommand = [commandCenter playCommand];
    [playCommand setEnabled:YES];
    [playCommand addTarget:self action:@selector(remotePlayEvent)];    
    MPRemoteCommand *nextCommand = [commandCenter nextTrackCommand];
    [nextCommand setEnabled:YES];
    [nextCommand addTarget:self action:@selector(remoteNextEvent)];    
    MPRemoteCommand *previousCommand = [commandCenter previousTrackCommand];
    [previousCommand setEnabled:YES];
    [previousCommand addTarget:self action:@selector(remotePreviousEvent)];    
    if (@available(iOS 9.1, *)) {        MPRemoteCommand *changePlaybackPositionCommand = [commandCenter changePlaybackPositionCommand];
        [changePlaybackPositionCommand setEnabled:YES];
        [changePlaybackPositionCommand addTarget:self action:@selector(remoteChangePlaybackPosition:)];
    }
}

在 iOS 7.1 之后,可以通过 MPRemoteCommandCenter 来控制音频播放。每个控制操作都封装为一个 MPRemoteCommand 对象,给 MPRemoteCommand 添加响应事件有两种方式:

一种是通过 addTargetWithHandler:,以 Block 的方式传入响应事件,需要返回 MPRemoteCommandHandlerStatusSuccess 来告知响应成功。

另一种是通过 addTarget: action:,因为 MPRemoteCommandCenter 是个单例,所以在 targetdealloc 中要记得调用 removeTarget: 。如下所示:

- (void)dealloc {

    [self removeCommandCenterTargets];
}

- (void)removeCommandCenterTargets {    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [[commandCenter playCommand] removeTarget:self];
    [[commandCenter pauseCommand] removeTarget:self];
    [[commandCenter nextTrackCommand] removeTarget:self];
    [[commandCenter previousTrackCommand] removeTarget:self];    
    if (@available(iOS 9.1, *)) {
        [commandCenter.changePlaybackPositionCommand removeTarget:self];
    }
}

注意:因为 changePlaybackPositionCommand 在 iOS 9.1 以后才可用,所以这里加了系统判断。

到这里就实现了锁屏界面的播放控制。

源码

请到 GitHub 上查看完整例子。

参考

iOS AVPlayer之后台连续播放视频
AVPlayer 音乐播放后台播放,以及锁屏主题设置
这可能是最详细的CMTime教程



作者:雷曼同学
链接:https://www.jianshu.com/p/71ab0e828c0a


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消