NCViewerVideo.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // NCViewerVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/07/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. import UIKit
  26. class NCViewerVideo: NSObject {
  27. @objc static let shared: NCViewerVideo = {
  28. let instance = NCViewerVideo()
  29. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  30. return instance
  31. }()
  32. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. private var imageView: UIImageView?
  34. private var image: UIImage?
  35. private var durationSeconds: Double = 0
  36. private var viewerVideoToolBar: NCViewerVideoToolBar?
  37. public var metadata: tableMetadata?
  38. public var videoLayer: AVPlayerLayer?
  39. public var player: AVPlayer?
  40. //MARK: - NotificationCenter
  41. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  42. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  43. self.player?.pause()
  44. }
  45. }
  46. func initVideoPlayer(imageView: UIImageView?, viewerVideoToolBar: NCViewerVideoToolBar?, metadata: tableMetadata) {
  47. guard let imageView = imageView else { return }
  48. if self.metadata == metadata { return }
  49. func initPlayer(url: URL) {
  50. self.player = AVPlayer(url: url)
  51. self.player?.isMuted = CCUtility.getAudioMute()
  52. self.player?.seek(to: .zero)
  53. self.videoLayer = AVPlayerLayer(player: self.player)
  54. self.videoLayer!.frame = imageView.bounds
  55. self.videoLayer!.videoGravity = .resizeAspect
  56. imageView.layer.addSublayer(videoLayer!)
  57. // At end go back to start & show toolbar
  58. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { (notification) in
  59. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  60. self.player?.seek(to: .zero)
  61. self.viewerVideoToolBar?.showToolBar(metadata: metadata)
  62. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  63. }
  64. }
  65. // save durationSeconds on database
  66. if let duration: CMTime = (player?.currentItem?.asset.duration) {
  67. durationSeconds = CMTimeGetSeconds(duration)
  68. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationSeconds: durationSeconds)
  69. }
  70. // NO Live Photo, seek to datamebase time
  71. if !metadata.livePhoto, let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  72. self.player?.seek(to: time)
  73. }
  74. viewerVideoToolBar?.setBarPlayer()
  75. }
  76. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  77. self.imageView = imageView
  78. self.image = imageView.image
  79. self.viewerVideoToolBar = viewerVideoToolBar
  80. self.metadata = metadata
  81. initPlayer(url: url)
  82. }
  83. }
  84. func videoPlay() {
  85. self.player?.play()
  86. }
  87. func videoPause() {
  88. guard let metadata = self.metadata else { return }
  89. self.player?.pause()
  90. if let time = self.player?.currentTime() {
  91. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
  92. }
  93. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  94. }
  95. func videoSeek(time: CMTime) {
  96. guard let metadata = self.metadata else { return }
  97. self.player?.seek(to: time)
  98. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
  99. }
  100. func videoRemoved() {
  101. videoPause()
  102. self.videoLayer?.removeFromSuperlayer()
  103. }
  104. func getVideoCurrentSeconds() -> Float64 {
  105. if let currentTime = NCViewerVideo.shared.player?.currentTime() {
  106. return CMTimeGetSeconds(currentTime)
  107. }
  108. return 0
  109. }
  110. func getVideoDurationSeconds() -> Float64 {
  111. return self.durationSeconds
  112. }
  113. }