Skip to content

Instantly share code, notes, and snippets.

let videoURL = URL(string: "Sample-Video-Url")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player.play()
}
let videoURL = URL(string: "Sample-Video-Url")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
func addPlayer(player: AVPlayer) {
player.currentItem?.seek(to: CMTime.zero, completionHandler: nil)
playerViewModel?.player = player
playerView.playerLayer.player = player
}
let exporter = AVAssetExportSession(asset: avUrlAsset, presetName: AVAssetExportPresetHighestQuality)
exporter?.outputURL = outputURL
exporter?.outputFileType = AVFileType.mp4
exporter?.exportAsynchronously(completionHandler: {
print(exporter?.status.rawValue)
print(exporter?.error)
})
if let cacheUrl = FindCachedVideoURL(forVideoId: videoId) {
let cacheAsset = AVURLAsset(url: cacheUrl)
asynchronouslyLoadURLAssets(cacheAsset)
}
else {
asynchronouslyLoadURLAssets(newAsset)
}
func removeVideoIfMaxNumberOfVideosReached() {
if popupVideosDict.count >= maxVideosAllowed {
// remove the least recently used video
let sortedDict = popupVideosDict.keysSortedByValue { (v1, v2) -> Bool in
v1.timeStamp < v2.timeStamp
}
guard let videoId = sortedDict.first else {
return
}
popupVideosDict.removeValue(forKey: videoId)
@objc func didTapSnap(_ sender: UITapGestureRecognizer) {
let touchLocation = sender.location(ofTouch: 0, in: view)
if touchLocation.x < view.frame.width/2 {
changePlayer(forward: false)
}
else {
fillupLastPlayedSnap()
changePlayer(forward: true)
}
}
static func findCachedVideoURL(forVideoId id: String) -> URL? {
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderPath).appendingPathComponent(id + ".mp4")
let filePath = fileURL.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
NewRelicService.sendCustomEvent(with: NewRelicEventType.statusCodes,
extension URL {
var attributes: [FileAttributeKey : Any]? {
do {
return try FileManager.default.attributesOfItem(atPath: path)
} catch let error as NSError {
print("FileAttribute error: \(error)")
}
return nil
}
@agammahajan1
agammahajan1 / grouping.swift
Created August 29, 2021 19:17
Grouping of view
cell.isAccessibilityElement = true
cell.accessibilityLabel = restaurantName + rating + offer