因为要写一个不占全面屏的视频,之前写的方法就不能用了,找到了一个神奇的控件,是一个大佬根据AVPlayer封装的。
代码如下:
//声明
var videoPlayer : BMPlayer!
//创建
videoPlayer = BMPlayer()
//设置视频网址,videoURL是视频链接,是字符串,name后填写视频的名称
let asset = BMPlayerResource(url: URL(string: videoURL)!, name: videoNAME, cover: nil, subtitle: nil)
videoPlayer.setVideo(resource: asset)
//添加到视图
self.view.addSubview(videoPlayer)
videoPlayer.snp.makeConstraints{ (make) -> Void in
make.top.left.right.equalTo(0)
make.height.equalTo(videoPlayer.snp.width).multipliedBy(9.0/16.0).priority(750)
}
videoPlayer.backBlock = { [unowned self] (isFullScreen) in
if isFullScreen == true{
return
}
let _ = self.navigationController?.popViewController(animated: true)
}
犯的错误如下:
1、在自动布局的时候,用videoPlayer.frame.width*(3/4)来设置,结果让视频高度为零,在视图上找不到该视频
2、我在页面下放了一个collectionView,在设置它的layout的时候,要用到 moreClctView = UICollectionView(frame: .zero, collectionViewLayout: layout),可以先将frame设置为.zero,然后再通过自动布局写collection的位置。
我学习BMPlayer的网址:https://www.ctolib.com/BMPlayer.html
这个网址内容更加全面!