Skip to content

Instantly share code, notes, and snippets.

@jasonyunjoonpark
Created March 2, 2018 19:55
Show Gist options
  • Save jasonyunjoonpark/cde67cd5597d31ec4bc6133ddb123747 to your computer and use it in GitHub Desktop.
Save jasonyunjoonpark/cde67cd5597d31ec4bc6133ddb123747 to your computer and use it in GitHub Desktop.
import AVFoundation
class LoginController: UIViewController {
var Player: AVPlayer!
var PlayerLayer: AVPlayerLayer!
// TITLE LABEL
let titleLabel: UILabel = {
let title = UILabel()
title.translatesAutoresizingMaskIntoConstraints = false
title.text = "Icarus"
title.textAlignment = .center
title.textColor = UIColor.white
title.font = UIFont(name: "AvenirNext-Italic" , size: 50)
return title
}()
override func viewDidLoad() {
super.viewDidLoad()
//setup
//setup video background view
setupVideoBackgroundView()
}
//setup title label constraints
fileprivate func setupTitleLabel() {
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: loginRegisterButton.topAnchor, constant: -300).isActive = true
titleLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -20).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
@objc func handlePlayerVideoEnded(notification: NSNotification) {
Player.seek(to: kCMTimeZero)
}
fileprivate func setupVideoBackgroundView() {
let url = Bundle.main.url(forResource: "video", withExtension: "mp4")
Player = AVPlayer.init(url: url!)
PlayerLayer = AVPlayerLayer(player: Player)
PlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
PlayerLayer.frame = view.layer.frame
Player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
Player.play()
view.layer.insertSublayer(PlayerLayer, at: 0)
NotificationCenter.default.addObserver(self, selector: #selector(handlePlayerVideoEnded(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: Player.currentItem)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment