Last active
April 11, 2023 14:21
-
-
Save egzonpllana/c8ae9b68369acf08f2a5715cb4fe9c86 to your computer and use it in GitHub Desktop.
Loading AVPlayer into UIView with Swift 5
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
// | |
// BackgroundVideoPlayerViewController.swift | |
// DeveloperBelt | |
// | |
// Created by Egzon Pllana on 10/7/19. | |
// Copyright © 2019 Native Coders. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
class BackgroundVideoPlayerViewController: UIViewController { | |
// MARK: - Properties | |
@IBOutlet weak var videoView: UIView! | |
private var playerItem: AVPlayerItem! | |
private var player: AVPlayer! | |
let center = NotificationCenter.default | |
// Observer should always be set | |
var playerObserver: NSKeyValueObservation! | |
// MARK: - Delegates | |
weak var pageViewController: OnBoardingPageViewController? | |
// MARK: - View Life cycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
// MARK: Hide navigation controller | |
self.navigationController?.navigationBar.isHidden = true | |
videoAnimationSetup() | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidDisappear(animated) | |
self.player.replaceCurrentItem(with: nil) /* deallocate memory */ | |
center.removeObserver(NSNotification.Name.AVPlayerItemDidPlayToEndTime) | |
playerObserver.invalidate() | |
} | |
// MARK: - Methods | |
// Configure the video animation | |
private func videoAnimationSetup() { | |
// retrive video from project files, type "mp4" | |
let videoURL = Bundle.main.url(forResource: "launch", withExtension: "mp4") | |
let asset = AVAsset(url: videoURL!) | |
// configure player | |
playerItem = AVPlayerItem(asset: asset) | |
observePlayer(playerItem) | |
player = AVPlayer(playerItem: playerItem) | |
let playerLayer = AVPlayerLayer(player: player) | |
// add player layer to view layer | |
playerLayer.frame = self.videoView.frame | |
playerLayer.videoGravity = .resizeAspectFill | |
self.videoView.layer.addSublayer(playerLayer) | |
// handle video end | |
center.addObserver(self, selector: #selector(videoEnded), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem) | |
} | |
// Observes when the player item is ready to play and handles automatic play back | |
private func observePlayer(_ playerItem: AVPlayerItem) { | |
playerObserver = playerItem.observe(\AVPlayerItem.status) { [weak self] (playerItem, _) in | |
if playerItem.status == .readyToPlay { | |
self?.player.play() | |
} | |
} | |
} | |
@objc private func videoEnded() { | |
playerItem.seek(to: CMTime.zero) { (finished) in | |
if finished { | |
OperationQueue.main.addOperation { [weak self] in | |
self?.player.play() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment