// // VideoViewController.swift // LMS // // Created by Suraj Kumar Mandal on 24/08/22. // import UIKit import SideMenu import AVKit import AVFoundation import HCVimeoVideoExtractor class VideoViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var thumbnailImageView: UIImageView! @IBOutlet var playButton: UIButton! var pageName = String() var videoId = String() var videoURL: URL? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. navigationBar.topItem?.title = pageName print(videoId) setupVideoPlayer() } func setupVideoPlayer() { HCVimeoVideoExtractor.fetchVideoURLFrom(id: videoId, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in if let err = error { print("Error = \(err.localizedDescription)") return } guard let vid = video else { print("Invalid video object") return } print("Title = \(vid.title), url = \(vid.videoURL), thumbnail = \(vid.thumbnailURL)") DispatchQueue.main.async() { self.videoURL = vid.videoURL[.quality360p] if let url = vid.thumbnailURL[.qualityBase] { self.thumbnailImageView.contentMode = .scaleAspectFit self.downloadImage(url: url) } } }) } func downloadImage(url: URL) { getDataFromUrl(url: url) { data, response, error in guard let data = data, error == nil else { return } DispatchQueue.main.async() { self.thumbnailImageView.image = UIImage(data: data) } } } func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) { URLSession.shared.dataTask(with: url) { data, response, error in completion(data, response, error) }.resume() } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ @IBAction func backAction(_ sender: UIBarButtonItem) { self.navigationController?.popViewController(animated: true) } @IBAction func playAction(_ sender: Any) { if let url = self.videoURL { let player = AVPlayer(url: url) let playerController = AVPlayerViewController() playerController.player = player self.present(playerController, animated: true) { player.play() } } } }