升級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

發佈了42 篇原創文章 · 獲贊 9 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章