OStack程序员社区-中国程序员成长平台

标题: ios - 将 AVPlayer subview 添加到 UIView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:26
标题: ios - 将 AVPlayer subview 添加到 UIView

我正在使用以下代码创建一个启用了分页的垂直 ScrollView ,效果非常好。现在我想将 AVPlayer (?) 类型的 subview 添加到 homeViewview2view3。我可以将 setupCustomPlayer() 放在 initiateScrollView() 函数中,该函数也非常有效。但是,它不附属于任何前面提到的观点。如果我尝试使用函数 addSubview(anything) 添加 subview ,它会告诉我需要一个 UIView。我需要 UIViews,因为垂直滑动提要非常重要。有人知道如何将其添加为 subview 吗?该视频将覆盖整个屏幕并用作人们可以说的背景视频(如果您想知道的话)

代码:

func setupCustomPlayer() {

    print("check")

    AVPlayerVC.view.frame = self.view.frame
    AVPlayerVC.view.sizeToFit()
    AVPlayerVC.showsPlaybackControls = false
    self.view.addSubview(AVPlayerVC.view)

    let videoURL = NSURL(string: "http://URL")


    let player = AVPlayer(url: videoURL! as URL)
    AVPlayerVC.player = player
    AVPlayerVC.player?.play()
}

func initiateScrollView() {

    //create scrollView with paging enabled
    let scrollView = UIScrollView(frame: view.bounds)
    scrollView.isPagingEnabled = true
    view.addSubview(scrollView)

    //do not show vertical scroll indicator
    scrollView.showsVerticalScrollIndicator = false;

    //get page size
    let pageSize = view.bounds.size

    //individual views
    let homeView = UIView()
    homeView.backgroundColor = .green

    let view2 = UIView()
    view2.backgroundColor = .blue

    let view3 = UIView()
    view3.backgroundColor = .red

    //array with individual views
    let pagesViews = [homeView, view2, view3]

    //amount of views
    let numberOfPages = pagesViews.count

    //add subviews (pages)
    for (pageIndex, page) in pagesViews.enumerated(){
        page.frame = CGRect(origin: CGPoint(x:0 , y:CGFloat(pageIndex) * pageSize.height), size: pageSize)
        scrollView.addSubview(page)
    }

    //define size of scrollView
    scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))

}



Best Answer-推荐答案


你能试试这个吗?

func setupCustomPlayer() {

    print("check")
    let player = AVPlayer(url: yourFileUrl)
    var playerLayer: AVPlayerLayer?
    playerLayer = AVPlayerLayer(player: player)
    playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    playerLayer!.frame = self.view.frame
    self.view.layer.addSublayer(playerLayer!)

    player.play()
}

关于ios - 将 AVPlayer subview 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968452/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4