AVPictureInPictureController无法在滚动过程中以编程方式激活

问题描述

我在一个单元格中有一个视频,如果我通过按钮将其置于PIP模式,则一切正常,但如果在单元格退出屏幕时以编程方式执行该操作,则不会自动置于PIP模式,则可以激活通过按钮吗?

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let cell = self.playerCell,self.playerController != nil else {
        return
    }
    
    let rect = self.view.convert(cell.frame,from: scrollView)
    if rect.origin.y < 0,self.pipController == nil {
        self.pipController = self.startPictureInPicture()
    }
}

func startPictureInPicture() -> AVPictureInPictureController? {
    guard AVPictureInPictureController.isPictureInPictureSupported() else {
        return nil
    }
    
    let layer = AVPlayerLayer(player: self.playerController.player)
    try? AVAudioSession.sharedInstance().setActive(true)
    if let pipController = AVPictureInPictureController(playerLayer: layer) {
        if pipController.isPictureInPicturePossible {
            pipController.startPictureInPicture()
        } else {
            pipController.addObserver(self,forKeyPath: "isPictureInPicturePossible",options: [.new,.initial],context: nil)
        }
    }
    
    return nil
}

override func observeValue(forKeyPath keyPath: String?,of object: Any?,change: [NSKeyValueChangeKey : Any]?,context: UnsafeMutableRawPointer?) {
    if keyPath == "isPictureInPicturePossible",let pipController = object as? AVPictureInPictureController,pipController.isPictureInPicturePossible {
        pipController.startPictureInPicture()
    }
}

更新:调试控制台始终显示警告用于的开始/结束外观转换的不平衡调用。但是我已经解决了:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    pipController.startPictureInPicture()
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)