VideoViewController.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // VideoViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 24/08/22.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import AVKit
  10. import AVFoundation
  11. import HCVimeoVideoExtractor
  12. class VideoViewController: UIViewController {
  13. @IBOutlet var navigationBar: UINavigationBar!
  14. @IBOutlet var thumbnailImageView: UIImageView!
  15. @IBOutlet var playButton: UIButton!
  16. var pageName = String()
  17. var videoId = String()
  18. var videoURL: URL?
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do any additional setup after loading the view.
  22. navigationBar.topItem?.title = pageName
  23. print(videoId)
  24. setupVideoPlayer()
  25. }
  26. func setupVideoPlayer() {
  27. HCVimeoVideoExtractor.fetchVideoURLFrom(id: videoId, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in
  28. if let err = error {
  29. print("Error = \(err.localizedDescription)")
  30. return
  31. }
  32. guard let vid = video else {
  33. print("Invalid video object")
  34. return
  35. }
  36. print("Title = \(vid.title), url = \(vid.videoURL), thumbnail = \(vid.thumbnailURL)")
  37. DispatchQueue.main.async() {
  38. self.videoURL = vid.videoURL[.quality360p]
  39. if let url = vid.thumbnailURL[.qualityBase] {
  40. self.thumbnailImageView.contentMode = .scaleAspectFit
  41. self.downloadImage(url: url)
  42. }
  43. }
  44. })
  45. }
  46. func downloadImage(url: URL) {
  47. getDataFromUrl(url: url) { data, response, error in
  48. guard let data = data, error == nil else { return }
  49. DispatchQueue.main.async() {
  50. self.thumbnailImageView.image = UIImage(data: data)
  51. }
  52. }
  53. }
  54. func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
  55. URLSession.shared.dataTask(with: url) { data, response, error in
  56. completion(data, response, error)
  57. }.resume()
  58. }
  59. /*
  60. // MARK: - Navigation
  61. // In a storyboard-based application, you will often want to do a little preparation before navigation
  62. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  63. // Get the new view controller using segue.destination.
  64. // Pass the selected object to the new view controller.
  65. }
  66. */
  67. @IBAction func backAction(_ sender: UIBarButtonItem) {
  68. self.navigationController?.popViewController(animated: true)
  69. }
  70. @IBAction func playAction(_ sender: Any) {
  71. if let url = self.videoURL {
  72. let player = AVPlayer(url: url)
  73. let playerController = AVPlayerViewController()
  74. playerController.player = player
  75. self.present(playerController, animated: true) {
  76. player.play()
  77. }
  78. }
  79. }
  80. }