Skip to content

Instantly share code, notes, and snippets.

@michaelevensen
Last active November 9, 2016 13:27
Show Gist options
  • Save michaelevensen/35af0d3794018310e8e23753ad99ecd4 to your computer and use it in GitHub Desktop.
Save michaelevensen/35af0d3794018310e8e23753ad99ecd4 to your computer and use it in GitHub Desktop.
// Load video
// Note: Needs to be a supported AVPlayer format, in this case MP4 (could also be M4V etc.)
if let filePath = Bundle.main.path(forResource: "video", ofType: "mp4") {
// url
let fileUrl = URL(fileURLWithPath: filePath)
// Player
let player = AVPlayer(url: fileUrl)
// video layer
let videoLayer = AVPlayerLayer(player: player)
videoLayer.frame = self.view.bounds
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
let videoView = UIView(frame: self.view.frame)
videoView.layer.addSublayer(videoLayer)
// Add video to backgroundView
self.view.addSubview(videoView)
self.view.sendSubview(toBack: videoView)
// play
player.play()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment