// // VideoAttachmentCollectionViewCell.swift // elpha-ios // // Created by Dwayne Harris on 11/29/18. // Copyright © 2018 Elpha. All rights reserved. // import AVFoundation import AVKit import UIKit class VideoAttachmentCollectionViewCell: UICollectionViewCell { var attachment: AttachmentMO? var player: AVQueuePlayer! var playerLayer: AVPlayerLayer! var playerLooper: AVPlayerLooper? override func awakeFromNib() { super.awakeFromNib() player = AVQueuePlayer() player.preventsDisplaySleepDuringVideoPlayback = false player.usesExternalPlaybackWhileExternalScreenIsActive = false player.isMuted = true playerLayer = AVPlayerLayer(player: player) playerLayer.videoGravity = .resizeAspectFill } func update(withAttachment attachment: AttachmentMO) { self.attachment = attachment layer.addSublayer(playerLayer) playerLayer.frame = bounds playerLooper = AVPlayerLooper(player: player!, templateItem: AVPlayerItem(url: attachment.url!)) if SettingsManager.automaticallyPlayGIFs { player.play() } } }