如果你的視頻使用的是HLS(m3u8)協(xié)議的,是不會由于升級ios10出現(xiàn)這個播放問題的。
1 解決:如果不是基于HLS協(xié)議的,解決方法如下
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進行播放
其中有幾個需要注意一下
- timeControlStatus
-
automaticallyWaitsToMinimizeStalling
第二個屬性默認為true,導(dǎo)致了不使用HLS的方式就會播放不了的問題。(比如,使用local Http Server間接實現(xiàn)在線播放、或下載到本地再播放)。
官網(wǎng)文檔描述如下:
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的播放狀態(tài)
在之前的版本中,我們通過rate來判斷avplayer是否處于播放中
- (Boolean)isPlaying
{
return self.player.rate==1;
}
iOS10中,AVPlayer新增方法timeControlStatus,我們就應(yīng)該這么實現(xiàn)
- (Boolean)isPlaying
{
if([[UIDevice currentDevice] systemVersion].intValue>=10){
return self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying;
}else{
return self.player.rate==1;
}
}
當(dāng)時的具體問題是這樣的,我們的場景是有聲繪本,在翻頁時使用了seek來設(shè)定起始時間。在調(diào)用了[self.player play]方法之后播放器并沒有播放(此時loadedTimeRanges已經(jīng)足以播放了) self.player.timeControlStatus 是wait狀態(tài)。
這時候其實可以通過另外一個iOS10新增加的屬性reasonForWaitingToPlay來查看wait的原因。
總的來說,如果在不使用HLS的情況下,AVPlayer新增加的屬性可以比較好的反應(yīng)實際播放中的一些狀態(tài),而無需我們自己去維護狀態(tài)了。
我們問題解決了。如果你的問題還沒有解決可以再看下這篇WWDC的文章,Advances in AVFoundation Playback。