Created
May 31, 2017 14:00
-
-
Save Savchukv/50ccf9610507f78c56eb1bc51d0337bf to your computer and use it in GitHub Desktop.
Play full screen video with routers
This file contains 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
// | |
// ViewController.swift | |
// | |
// Created by Vasiliy Savchuk on 19.01.17. | |
// Copyright © 2017 All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
import AVKit | |
class VideoViewController: UIViewController { | |
var avPlayer: AVPlayer! | |
var avPlayerLayer: AVPlayerLayer! | |
var router: AuthorizationRouter! | |
//MARK - Lifecicle Object | |
deinit { | |
NotificationCenter.default.removeObserver(self, | |
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, | |
object: avPlayer.currentItem) | |
} | |
override func viewDidLoad() { | |
self.setupAVPlayer() | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(playerItemDidReachEnd(notification:)), | |
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, | |
object: avPlayer.currentItem) | |
avPlayer.play() | |
} | |
//MARK: - Methods | |
func playerItemDidReachEnd(notification: Notification) { | |
let playerVideoItem: AVPlayerItem = notification.object as! AVPlayerItem | |
playerVideoItem.seek(to: kCMTimeZero) | |
} | |
func setupAVPlayer() { | |
let theURL = Bundle.main.url(forResource: "video", withExtension: "mp4")! | |
avPlayer = AVPlayer(url: theURL) | |
avPlayerLayer = AVPlayerLayer(player: avPlayer) | |
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
avPlayer.volume = 0 | |
avPlayer.actionAtItemEnd = .pause | |
avPlayerLayer.frame = view.layer.bounds | |
view.backgroundColor = .clear | |
view.layer.insertSublayer(avPlayerLayer, at: 0) | |
} | |
//MARK: - Actions | |
@IBAction func skipVideoButtonDidPress(_ sender: Any) { | |
self.router?.backTo() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment