最近項(xiàng)目中想重新統(tǒng)一視頻播放,在此開(kāi)發(fā)中遇到一些問(wèn)題,在此記錄,亦供大家參考。
一
- 錯(cuò)誤信息
player item failed:Error Domain=AVFoundationErrorDomain Code=-11839 "Cannot Decode" UserInfo={NSUnderlyingError= 0x123456 {Error Domain=NSOSStatusErrorDomain Code=-12913 "(null)"}, NSLocalizedFailureReason=The decoder required for this media is busy., NSLocalizedRecoverySuggestion=Stop any other actions that decode media and try again., NSLocalizedDescription=Cannot Decode}
原因
? “內(nèi)存泄露”(可能性最大)或者 “人為故意” 導(dǎo)致AVPlayer通道創(chuàng)建過(guò)多!被AVFoundation限制解碼。
?由于iOS硬件的局限性,AVFoundation限制了當(dāng)前設(shè)備中視頻播放器的支持?jǐn)?shù)量,據(jù)相關(guān)資料顯示這個(gè)數(shù)量是4,即同時(shí)存在5個(gè)播放器實(shí)例則就可能得到該錯(cuò)誤。
?當(dāng)然,以上的限制不是說(shuō)你不能新建超過(guò)4個(gè)AVPlayer實(shí)例,而是指“渲染通道”。調(diào)用方法:
AVPlayer * player = [AVPlayer playerWithPlayerItem:somePlayerItem];
即會(huì)生成一條新的渲染通道。參考
Error Code Meaning
AVPlayerItem fails with AVStatusFailed and error code “Cannot Decode”
How many AVPlayers are allowed to be created at the same time?
iOS 究極體測(cè)試工具 內(nèi)存泄漏
MLeaksFinder:精準(zhǔn) iOS 內(nèi)存泄露檢測(cè)工具
二
- 錯(cuò)誤信息
An instance 0x1700196c0 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x17045bff0> ( <NSKeyValueObservance 0x1702d30f0: Observer: 0x136fd6930, Key path: status, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x17465eab0> <NSKeyValueObservance 0x1704c0000: Observer: 0x136fd6930, Key path: loadedTimeRanges, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x170852ba0> )
原因
提示:在我們項(xiàng)目中這個(gè)錯(cuò)誤只出現(xiàn)在iOS8系統(tǒng)上,iOS8以上的系統(tǒng)并沒(méi)有發(fā)現(xiàn)相關(guān)問(wèn)題。
?出現(xiàn)這個(gè)問(wèn)題是在iOS8斷網(wǎng)情況下點(diǎn)擊播放視頻,AVPlayerItem實(shí)例回調(diào)Observer顯示失敗狀態(tài)后直接崩潰,Bugly提示信息顯示當(dāng)前的AVPlayerItem已經(jīng)被回收,但是卻沒(méi)有移除相關(guān)的通知監(jiān)聽(tīng),導(dǎo)致崩潰。但AVPlayer我們是有采用強(qiáng)引用的,因此暫不明確為什么會(huì)被系統(tǒng)回收了。
?暫時(shí)的解決方案是創(chuàng)建一個(gè)強(qiáng)指針變量來(lái)引用該AVPlayerItem實(shí)例,再在合適的時(shí)間去手動(dòng)清除引用。
若有人知道更深層的原因或者更好的解決方法,還請(qǐng)不吝賜教。參考
AVPlayerItem was deallocated while key value observers were still registered
三
- 錯(cuò)誤信息
Error Domain=AVFoundationErrorDomain Code=-11800 "這項(xiàng)操作無(wú)法完成" UserInfo=0x175271340 {NSUnderlyingError=0x17485bba0 "未能完成操作。(“OSStatus”錯(cuò)誤 -12983。)", NSLocalizedFailureReason=發(fā)生未知錯(cuò)誤(-12983), NSLocalizedDescription=這項(xiàng)操作無(wú)法完成}
-
原因
這個(gè)錯(cuò)誤網(wǎng)上沒(méi)有統(tǒng)一明確的解決方法,在此只做個(gè)人摘記和總結(jié)。
?1.內(nèi)存泄露:內(nèi)存泄露導(dǎo)致AVPlayer沒(méi)有被釋放。但是并沒(méi)有找到官方解釋內(nèi)存泄露為什么會(huì)導(dǎo)致這個(gè)錯(cuò)誤產(chǎn)生。
?2.AVPlayer移除不完全,導(dǎo)致重復(fù)添加。解決方法是完全移除后釋放AVPlayer實(shí)例再重新添加。詳情見(jiàn)以下參考第一條。
?3.存儲(chǔ)的文件路徑NSURL有問(wèn)題。即使你打印出來(lái)沒(méi)有什么問(wèn)題。解決辦法是重寫(xiě)輸出文件的路徑NSURL。此種原因限于本地錄制再使用,至于播放遠(yuǎn)程視頻貌似沒(méi)有作用。重寫(xiě)路徑參考代碼如下:
NSString *documentPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSURL *documentUrl = [NSURL fileURLWithPath:documentPath isDirectory:YES]; self.saveMovieFile = [NSURL URLWithString:@"video.mp4" relativeToURL:documentUrl];
若有人知道更深層的原因或者更好的解決方法,還請(qǐng)不吝賜教。 -
參考
AVPlayer fails with AVPlayerItemStatusFailed (OSStatus error -12983)
[ios]AVPlayerItemStatusFailed (OSStatus 錯(cuò)誤-12983) AVPlayer 失敗
ios視頻保存Error Domain未知錯(cuò)誤
iOS視頻一直播放失敗,視頻加載失敗
四
- 錯(cuò)誤信息
-[AVPlayerItem seekToTime:toleranceBefore:toleranceAfter:completionHandler:] Seeking is not possible to time {INVALID}
-
原因
? 在我當(dāng)前的項(xiàng)目中,Bugly反饋這個(gè)錯(cuò)誤有點(diǎn)并不準(zhǔn)確,因?yàn)閷?shí)際上我使用的是方法是:
seekToTime:completionHandler:
? 根據(jù)反饋很容易發(fā)現(xiàn)是seek的CMTime結(jié)構(gòu)體是無(wú)效的,因此最直接的解決方法是新增過(guò)濾,系統(tǒng)提供了CMTIME_IS_VALID(time)與CMTIME_IS_INVALID(time)兩個(gè)宏可以用來(lái)判斷。
?stackoverflow上建議使用seekToTime:toleranceBefore:toleranceAfter:completionHandler:方法進(jìn)行更精確的seeking。
若有人知道更深層的原因或者更好的解決方法,還請(qǐng)不吝賜教。 -
參考
AVPlayer seekToTime: backward not working
davidlondono/PlayerView
seekToTime:toleranceBefore:toleranceAfter:completionHandler: