Hello. I am trying to have a video in the background in my initial ViewController. So far, I have this:
import UIKit
import AVFoundation
import AVKit
class IGASplashVC: UIViewController
{
var player: AVPlayer?
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
setupVideoBackground()
// Draws buttons
drawOutlets()
}
func setupVideoBackground()
{
let path = NSBundle.mainBundle().pathForResource("launch", ofType: "mp4")
if let path = path
{
let url = NSURL(fileURLWithPath: path)
self.player = AVPlayer(URL: url)
self.player!.actionAtItemEnd = AVPlayerActionAtItemEnd.None
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.frame = self.view.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.insertSublayer(playerLayer, atIndex: 0)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.player!.currentItem)
self.player!.seekToTime(kCMTimeZero)
self.player!.play()
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -10
verticalMotionEffect.maximumRelativeValue = 10
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -10
horizontalMotionEffect.maximumRelativeValue = 10
let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
self.view.addMotionEffect(group)
}
}
func playerItemDidReachEnd()
{
self.player!.seekToTime(kCMTimeZero)
}
}
However, it is just black screen, no video. Thanks for your help in advance!