Last active
February 16, 2017 12:20
-
-
Save michaelevensen/9fe5e9e985846dff3196a73e0556970c to your computer and use it in GitHub Desktop.
Video embed example with AVKit and AVFoundation. Loops video ∞.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
@IBOutlet weak var videoOverlayView: UIView! | |
@IBOutlet weak var videoView: UIView! | |
var player: AVPlayer? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Load video resource | |
if let videoUrl = Bundle.main.url(forResource: "video_file_name", withExtension: "mp4") { | |
// Init video | |
self.player = AVPlayer(url: videoUrl) | |
self.player?.isMuted = true | |
self.player?.actionAtItemEnd = .none | |
// Add player layer | |
let playerLayer = AVPlayerLayer(player: player) | |
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
playerLayer.frame = view.frame | |
// Add video layer | |
self.videoView.layer.addSublayer(playerLayer) | |
// Play video | |
self.player?.play() | |
// Observe end | |
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem) | |
} | |
} | |
// MARK: - Loop video when ended. | |
func playerItemDidReachEnd(notification: NSNotification) { | |
self.player?.seek(to: kCMTimeZero) | |
self.player?.play() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment