【问题标题】:[iOS]: Picture in picture not working in UIKit class using UIViewControllerRepresentable[iOS]:画中画在使用 UIViewControllerRepresentable 的 UIKit 类中不起作用
【发布时间】:2022-11-12 14:02:43
【问题描述】:

我正在尝试通过以下设置实现画中画自定义播放器:

private func setupCustomPlayer(){
    let playerLayer = AVPlayerLayer(player: player)
        
    playerLayer.frame = videoView.bounds
    playerLayer.videoGravity = .resizeAspectFill
    videoView.layer.addSublayer(playerLayer)
    
    pip = AVPictureInPictureController(playerLayer: playerLayer)
    pip.canStartPictureInPictureAutomaticallyFromInline = true
    pip.delegate = self
    
    player?.play()
}

但是,画中画不起作用,我还在应用委托中设置了音频会话:

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playback, mode: .moviePlayback)
    } catch {
        print("Setting category to AVAudioSessionCategoryPlayback failed.")
    }

问题是。可能会发生什么。我正在使用此源代码来指导我:https://github.com/sharmavipin11289/PIP,但我没有运气。

有我的 UIViewControllerRepresentable 类:

func makeUIViewController(context: Context) -> some PlayerViewController {
    let vc = PlayerViewController(url: url, showsHelp: false) {
        fullscreen.toggle()
        if fullscreen {
            self.changeOrientation(to: .landscapeLeft)
        } else {
            self.changeOrientation(to: .portrait)
        }
    }
    return vc
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    
}

有谁能够帮我?

此致

【问题讨论】:

  • 您还需要在后台模式下启用画中画检查此链接:link。也可以尝试设置 .longFormAudio 而不是 moviePlayback
  • 嗨 @Mr.SwiftOak 我试过了但没有任何效果,但在一个简单的 AVPlayerController 中工作。会发生什么?

标签: ios swift swiftui avkit


【解决方案1】:

从 iOS 14 升级到 iOS 16 并在 App Delegate 中使用以下音频会话可以解决该问题:

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

【讨论】:

    猜你喜欢
    • 2014-07-31
    • 1970-01-01
    • 2015-06-06
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    相关资源
    最近更新 更多