AVPlayer在iOS10.3 replaceCurrentItem 崩潰問題的解決辦法

AVPlayer在iOS10.3 replaceCurrentItem 崩潰問題的解決辦法

AVPlayer在實(shí)現(xiàn)連續(xù)播放使用replaceCurrentItem來切換音頻的時(shí)候,在iOS10.3上會(huì)出現(xiàn)閃退。

self.player.replaceCurrentItem(with: playerItem)

錯(cuò)誤內(nèi)容如下

invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

解決辦法移除CurrentItem的監(jiān)聽,重新添加即可

private func currentItemAddObserver(){
        addObserver = true
        //監(jiān)控狀態(tài)屬性,注意AVPlayer也有一個(gè)status屬性,通過監(jiān)控它的status也可以獲得播放狀態(tài)
        player.currentItem?.addObserver(self, forKeyPath: "status", options: .new , context: nil)
        //監(jiān)控緩沖加載情況屬性
        player.currentItem?.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.new, context: nil)
        //監(jiān)控播放完成通知
        NotificationCenter.default.addObserver(self, selector: #selector(playbackFinished(noti:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        //監(jiān)控時(shí)間進(jìn)度
        timeObserver = player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: DispatchQueue.main, using: { (time : CMTime) in
            
        })
    }
    
    private func currentItemRemoveObserver(){
        if addObserver {
            addObserver = false
            guard let currentItem = player.currentItem else { return }
            currentItem.removeObserver(self, forKeyPath: "status")
            currentItem.removeObserver(self, forKeyPath: "loadedTimeRanges")
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
            if let observer = self.timeObserver  {
                player.removeTimeObserver(self.timeObserver)
            }
        }
        
    }
    
    currentItemRemoveObserver()
        
        self.player.replaceCurrentItem(with: playerItem)
        if #available(iOS 10.0, *) {
            self.player.automaticallyWaitsToMinimizeStalling = false
        }
        currentItemAddObserver()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容