我正在控制页面控制,并且在每个页面中都会播放视频,但是我已经使用了xibs,在每个xib中加载每个视频,只有第一个视频正在播放,所有视频都没有播放,显示为空白屏幕。
主要代码
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadScrollView()
}
func loadScrollView() {
let pageCount : CGFloat = 3.0
scrollView.backgroundColor = UIColor.clear
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * pageCount, height: scrollView.frame.size.height)
scrollView.showsHorizontalScrollIndicator = false
pageController.numberOfPages = Int(pageCount)
let v1 = Video1.instanceFromNib() // xib with video 1
let v2 = Video2.instanceFromNib() // xib with video 2
let v3 = Video3.instanceFromNib() // xib with video 3
var videoClassArray = [v1,v2,v3] as [UIView]
for i in 0..<Int(pageCount) {
print(self.scrollView.frame.size.width)
var view = UIView(frame: CGRect(x: self.scrollView.frame.size.width * CGFloat(i), y: 0, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height))
view = videoClassArray[i]
self.scrollView.addSubview(view)
}
}
//MARK: UIScrollView Delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let viewWidth: CGFloat = scrollView.frame.size.width
// content offset - tells by how much the scroll view has scrolled.
let pageNumber = floor((scrollView.contentOffset.x - viewWidth / 50) / viewWidth) + 1
pageController.currentPage = Int(pageNumber)
}
//MARK: Page tap action
func pageChanged() {
let pageNumber = pageController.currentPage
var frame = scrollView.frame
frame.origin.x = frame.size.width * CGFloat(pageNumber)
frame.origin.y = 0
scrollView.scrollRectToVisible(frame, animated: true)
}
以上是视图控制器,我将xib加载到各种视图中.xib代码如下所示。
class Video1: UIView {
class func instanceFromNib() -> UIView {
return UINib(nibName: "Video1", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
var player: AVPlayer!
var playerLayer = AVPlayerLayer()
var videoName = "BoardSetup_1"
@IBOutlet weak var baseView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
let videoURL = Bundle.main.url(forResource: videoName, withExtension: "mp4")
player = AVPlayer(url: videoURL!)
player?.actionAtItemEnd = .none
player?.isMuted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -1
playerLayer.bounds = self.layer.bounds
playerLayer.frame = baseView.frame
baseView.layer.addSublayer(playerLayer)
baseView.backgroundColor = UIColor.blue
player?.play()
//loop video
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.player?.seek(to: kCMTimeZero)
self.player.play()
}
})
}
}
以上代码适用于加载视频的xib,其余xib的代码相同,因此仅提及1个xib。 加载xib时面临的问题只有视频会在第一个视图中播放,在休息视图中显示为空白屏幕。