【发布时间】: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 中工作。会发生什么?