如何在UIView swift中播放视频? [英] how to play a video in UIView swift?
问题描述
我有一个视频,我想从服务器下载并以固定视图流式传输. 我在情节提要中设置了具有固定约束的UIView,这是我在代码中所做的事情:
I have a video which I want to download from a server and stream it in a fixed view. I've set a UIView in my storyboard with fixed constraints, and here is what I've done in code:
@IBOutlet weak var videoView: UIView!
var player: AVPlayer!
var avpController = AVPlayerViewController()
在我的viewDidLoad
中,我已经这样做了:
And in my viewDidLoad
I've done this:
let url = URL(string:myURL)
player = AVPlayer(url: url!)
avpController.player = player
avpController.videoGravity = AVLayerVideoGravity.resizeAspect.rawValue
self.addChildViewController(avpController)
avpController.view.frame = videoView.frame
self.containerView.addSubview(avpController.view)
videoView.layer.masksToBounds = true
我的问题是我的视频尺寸不符合我设置为videoView
的尺寸,并且在每个设备中我的视频尺寸都不一样.在某些设备中,视频高度大于我设置的高度,并且覆盖了我在videoView
以下的项目.如何以正确的方式在视图中播放视频?
My problem is my video is not with the size that I've set to videoView
and in every device my video is in a different size. In some devices, the video height is larger than the height that I've set and it overlays the items that I have below videoView
. How can I play video in a view in a right way?
推荐答案
在情节提要中将视频添加到具有固定约束的UIView的唯一方法是:
the only way to add a video to a UIView with fixed constraints in storyboard, was this :
let url = URL(string:myURL)
player = AVPlayer(url: url!)
avpController.player = player
avpController.view.frame.size.height = videoView.frame.size.height
avpController.view.frame.size.width = videoView.frame.size.width
self.videoView.addSubview(avpController.view)
我希望其他人可以使用它! :)
I hope other can use this! :)
这篇关于如何在UIView swift中播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!