gpt4 book ai didi

ios - 为什么画中画在iOS启动片刻后立即停止?

转载 作者:行者123 更新时间:2023-12-05 04:42:31 32 4
gpt4 key购买 nike

当我从我的自定义视频播放器启动画中画时,pip 启动了片刻然后失败。我在调试控制台中发现了如下错误日志:

PGPictureInPictureProxy (0x12710a280) _updateAutoPIPSettingsAndNotifyRemoteObjectWithReason:] - Acquiring remote object proxy for connection <NSXPCConnection: 0x2825a32a0> connection to service with pid 63 named com.apple.pegasus failed with error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service with pid 63 named com.apple.pegasus was invalidated from this process." UserInfo={NSDebugDescription=The connection to service with pid 63 named com.apple.pegasus was invalidated from this process.}

这是我在代码中管理 pip 的方式:-

CustomVideoPlayer 设置 pip vc 的代码:

    private func setupPIPIfEligible() {
if AVPictureInPictureController.isPictureInPictureSupported() {
// Create a new controller, passing the reference to the AVPlayerLayer.
pipVC = nil
if let layer = playerLayer {
pipVC = AVPictureInPictureController(playerLayer: layer)
pipVC?.delegate = self
}
}}

CustomVideoPlayer 在按下按钮时切换 pip 的代码:

private func togglePIP() {
if pipVC?.isPictureInPictureActive ?? false {
pipVC?.stopPictureInPicture()
} else {
pipVC?.startPictureInPicture()
}
}

自定义视频播放器 VC 将 pip 委托(delegate)方法转发给它的委托(delegate):

extension CustomVideoPlayerViewController: AVPictureInPictureControllerDelegate {
func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
videoPlayerDelegate?.playerViewControllerWillStartPictureInPicture(self)
}

func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
videoPlayerDelegate?.playerViewControllerWillStopPictureInPicture(self)
}

func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
videoPlayerDelegate?.playerViewController(self, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler: completionHandler)
}}

CustomVideoPlayerViewController 的委托(delegate)处理 pip 方法:

extension TabBarViewController: CustomVideoPlayerDelegate {

func playerViewController(_ playerViewController: UHVideoPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
shouldReleaseVideoPlayer = false
let currentviewController = navigationController?.visibleViewController
if currentviewController != playerViewController, currentviewController != nil {
if !(currentviewController?.isKind(of: CustomVideoPlayerViewController.self) ?? false) {
currentviewController?.present(playerViewController, animated: false) {

}
}
}
}

func playerViewControllerWillStartPictureInPicture(_ playerViewController: UHVideoPlayerViewController) {
playerViewController.dismiss(animated: true, completion: nil)
}

func playerViewControllerWillStopPictureInPicture(_ playerViewController: UHVideoPlayerViewController) {
if shouldReleaseVideoPlayer {
currentVideoPlayer = nil
}
shouldReleaseVideoPlayer = true
}}

var currentVideoPlayer: CustomVideoPlayerViewController?是我为视频播放器保留的有力引用。

我已经检查过:没有内存泄漏/视频播放器引用的提前发布。

我做错了什么?

最佳答案

不要忘记,如果您使用的不是 AVPlayerViewController 而是一些自定义 View ,那么在画中画模式下播放视频时,您需要让 AVPLayerLayer 显示在屏幕上自己的层作为 AVPLayerLayer。如果您从屏幕上删除自定义 View ,画中画视频也将结束。您还需要设置 AVAudioSession 来播放长格式音频,将此代码添加到您的 AppDelegate 类:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupAudioSession()
return true
}

private func setupAudioSession() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playback)
try audioSession.setActive(true, options: [])
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
}

编辑:

如果用户转到后台,画中画的意义仍然存在,视频将继续在其他应用程序之上的画中画窗口中播放。好吧,如果您想通过所有应用程序屏幕呈现画中画视频,您基本上只有两个选项(据我所知)。要么使用 AVPlayerViewController ,无论您当前在哪个屏幕上,它都会继续播放视频,或者在您的应用程序的关键窗口中显示视频,如果用户导航到其他屏幕,您可以缩小 AVPlayerlayer 的大小并在那里显示视频(就像 YouTube 那样),或触发画中画,但在应用程序底部的某处保留一小部分 AVPlayerlayer。还有第三个选项(但我还没有尝试过那个),它将您的自定义视频 View Controller 合并为其他一些呈现的 View Controller 的子级。但是我有一种感觉,以这种方式使用它可能会很痛苦。希望这些回答对您有所帮助。祝你好运!

关于ios - 为什么画中画在iOS启动片刻后立即停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69820836/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com