简体   繁体   English

将AVPlayerViewController视图添加到外部窗口

[英]add AVPlayerViewController view to external window

I want to programmatically add an AVPlayerViewController view to an external window from an iPad. 我想以编程方式从iPad将AVPlayerViewController视图添加到外部窗口。 The problem is that when adding it, the player view says "This video is playing on the TV" 问题是,添加时,播放器视图显示“此视频正在电视上播放”

example code: 示例代码:

let player = AVPlayer(url: someUrl)
//these two properties seem to have no effect
//player.usesExternalPlaybackWhileExternalScreenIsActive = true/false
//player.allowsExternalPlayback = true/false
let viewController = AVPlayerViewController()
viewController.player = player
viewController.view.frame = frame
externalWindow.addSubview(viewController.view)
player.play()

Is there a way to add the video view myself to the external window so I can decide the size? 有没有办法自己将视频视图添加到外部窗口,以便我确定尺寸?

If you want to control the size of the AVPlayerViewController you can use the layer or you can add it as a childViewController 如果要控制AVPlayerViewController的大小,则可以使用该图层,也可以将其添加为childViewController

Using Layer 使用图层

let videoURL = URL(url to your file)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()

In the above code the frame can be adjusted as per the requirement. 在上面的代码中,可以根据需要调整框架。

Using ChildViewController: 使用ChildViewController:

let videoURL = URL(fileURLWithPath: videoPath)
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = CGRect(give the appropriate size)
playerViewController.player = player  

self.addChildViewController(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)

Let me know if this helps!! 让我知道这是否有帮助! or anything else is needed. 或其他任何需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM