最近在項目中使用AVPlayer中發(fā)現(xiàn)了一個錯誤:
Error Domain=AVFoundationErrorDomain Code=-11839 "無法解碼" UserInfo={NSLocalizedDescription=無法解碼, NSUnderlyingError=0x1c1851f10 {Error Domain=NSOSStatusErrorDomain Code=-12913 "(null)"}, NSLocalizedRecoverySuggestion=停止其他所有對媒體解密的操作,然后再試一次。, NSLocalizedFailureReason=此媒體所需的解碼器正忙。}
在APP運行期間, 只要執(zhí)行這個方法, 超過16次, 就會出現(xiàn)這個這個錯誤.
self.plyer = [AVPlayer playerWithPlayerItem:playerItem];
這個錯誤會導(dǎo)致AVPlayer無法播放視頻.
在網(wǎng)上搜了一圈后, 發(fā)現(xiàn)這個錯誤是由于內(nèi)存泄漏造成了.因為我在使用AVPlayer的時候,每次創(chuàng)建完之后,沒有釋放掉它,導(dǎo)致錯誤產(chǎn)生.
釋放AVPlayer其實很簡單, 在每次離開播放頁面, 或者不需要播放的時候, 加上這行代碼就行. 之后無論你怎么創(chuàng)建AVPlayer, 都不會再出現(xiàn)這個無法解碼問題
[self.player replaceCurrentItemWithPlayerItem:nil];