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
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() | |
} |
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
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() |
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
func addPlayer(player: AVPlayer) { | |
player.currentItem?.seek(to: CMTime.zero, completionHandler: nil) | |
playerViewModel?.player = player | |
playerView.playerLayer.player = player | |
} |
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
@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) | |
} | |
} |
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
func asynchronouslyLoadURLAssets(_ newAsset: AVURLAsset) { | |
DispatchQueue.main.async { | |
newAsset.loadValuesAsynchronously(forKeys: self.assetKeysRequiredToPlay) { | |
for key in self.assetKeysRequiredToPlay { | |
var error: NSError? | |
if newAsset.statusOfValue(forKey: key, error: &error) == .failed { | |
self.delegate?.playerDidFailToPlay(message: "Can't use this AVAsset because one of it's keys failed to load") | |
return | |
} | |
} |
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
let exporter = AVAssetExportSession(asset: avUrlAsset, presetName: AVAssetExportPresetHighestQuality) | |
exporter?.outputURL = outputURL | |
exporter?.outputFileType = AVFileType.mp4 | |
exporter?.exportAsynchronously(completionHandler: { | |
print(exporter?.status.rawValue) | |
print(exporter?.error) | |
}) |
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
if let cacheUrl = FindCachedVideoURL(forVideoId: videoId) { | |
let cacheAsset = AVURLAsset(url: cacheUrl) | |
asynchronouslyLoadURLAssets(cacheAsset) | |
} | |
else { | |
asynchronouslyLoadURLAssets(newAsset) | |
} |
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
func isStorageAvailable() -> Bool { | |
let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String) | |
do { | |
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey, .volumeTotalCapacityKey]) | |
guard let totalSpace = values.volumeTotalCapacity, | |
let freeSpace = values.volumeAvailableCapacityForImportantUsage else { | |
return false | |
} | |
if freeSpace > minimumSpaceRequired { | |
return true |
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
func cleanExpiredVideos() { | |
let currentTimeStamp = Date().timeIntervalSince1970 | |
var expiredKeys: [String] = [] | |
for videoData in videosDict where currentTimeStamp - videoData.value.timeStamp >= expiryTime { | |
// video is expired. delete | |
if let _ = popupVideosDict[videoData.key] { | |
expiredKeys.append(videoData.key) | |
} | |
} | |
for key in expiredKeys { |
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
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) |