升级iOS10后,AVPlayer有时候播放延时和播放不了的问题

如果你的视频使用的是HLS(m3u8)协议的,是不会由于升级ios10出现这个播放问题的。

如果不是基于HLS协议的,解决方法如下

self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
//        [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
if([[UIDevice currentDevice] systemVersion].intValue>=10){
//      增加下面这行可以解决iOS10兼容性问题了
      self.player.automaticallyWaitsToMinimizeStalling = NO;
}

ios10种AVPlayer增加了多个属性,其中有几个需要注意一下

  1. timeControlStatus
  2. automaticallyWaitsToMinimizeStalling
    正是第二个属性的默认值导致了不使用HLS的方式(比如使用localhttpserver间接实现在线播放、或者下载到本地再播放的方式)就会播放不了的问题。

重点就在上述第二条的官网文档的这段描述

Important

For clients linked against iOS 10.0 and later or macOS 10.12 and later (and running on those versions), the default value of this property is true. This property did not exist in previous OS versions and the observed behavior was dependent on the type of media played:

* HTTP Live Streaming (HLS): When playing HLS media, the player behaved as if automaticallyWaitsToMinimizeStalling is true.

* File-based Media: When playing file-based media, including progressively downloaded content, the player behaved as if automaticallyWaitsToMinimizeStalling is false.

You should verify that your playback applications perform as expected using this new default automatic waiting behavior.

在之前的版本中,我们通过rate来判断avplayer是否处于播放中

- (Boolean)isPlaying
{
    return self.player.rate==1;
}

在iOS10中,AVPlayer多了一个timeControlStatus,我们就应该这么实现

- (Boolean)isPlaying
{
    if([[UIDevice currentDevice] systemVersion].intValue>=10){
        return self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying;
    }else{
        return self.player.rate==1;
    }
}

当时的具体问题是这样的,我们的场景是有声绘本,在翻页时使用了seek来设定起始时间。在调用了[self.player play]方法之后播放器并没有播放(此时loadedTimeRanges已经足以播放了) self.player.timeControlStatus 是wait状态。
这时候其实可以通过另外一个iOS10新增加的属性reasonForWaitingToPlay来查看wait的原因。

总的来说,如果在不使用HLS的情况下,AVPlayer新增加的属性可以比较好的反应实际播放中的一些状态,而无需我们自己去维护状态了。

我们问题解决了。如果你的问题还没有解决可以再看下这篇WWDC的文章,Advances in AVFoundation Playback

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值