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

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

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

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

2 原因:iOS10中AVPlayer新增属性的默认值,默认你使用HLS进行播放

其中有几个需要注意一下

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

官网文档描述如下:

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.

3 新版本中判断AVPlayer的播放状态

在之前的版本中,我们通过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。

你可能感兴趣的:(升级iOS10后,AVPlayer有时候播放不了的问题)