【问题标题】:Embed a ViewController into a AVPlayerController as an Overlay将 ViewController 作为 Overlay 嵌入到 AVPlayerController 中
【发布时间】:2020-03-20 21:25:11
【问题描述】:

我的问题:我正在尝试将 ViewController(带有图像和按钮)嵌入到 AVPlayerController 中,类似于 YouTube 在其视频上展示广告的方式(位于视频,并将留在那里)。

我的方法:我有以下测试代码

let AVPC = AVPlayerViewController()
            avpc!.player = self.contentPlayer
            avpc!.view.frame = f
            self.addChild(avpc!)
let adView = UIView()
adView.frame = CGRect(x: ??, y: ??, width: 300, height: 70)
AVPC.contentOverlayView.addSubview(adView)

结果:我的视图将在最小化时正确计算我的 AVPlayerController 的大小。但是,当我进入全屏视图时,它要么超出范围,要么根本不工作。

模拟

【问题讨论】:

  • 您需要为adView设置自动布局,以根据您的播放器大小进行更改

标签: ios swift autolayout avplayer avplayerviewcontroller


【解决方案1】:

你应该看看autolayout,还有一些代码更少的自动布局工具,比如SnapKit

这是一个无需任何库即可处理它的示例方法。

class ViewController: UIViewController {

    let adView: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        return view
    }()

    func playVideo() {
        let videoURL = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        playerViewController.contentOverlayView?.addSubview(adView)

        adView.translatesAutoresizingMaskIntoConstraints = false
        adView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        adView.bottomAnchor.constraint(equalTo: adView.superview!.bottomAnchor, constant: -20).isActive = true
        adView.leadingAnchor.constraint(equalTo: adView.superview!.leadingAnchor, constant: 40).isActive = true
        adView.trailingAnchor.constraint(equalTo: adView.superview!.trailingAnchor, constant: -40).isActive = true

        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 2020-11-14
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多