|
@@ -1,5 +1,5 @@
|
|
|
//
|
|
|
-// NCViewerVideo.swift
|
|
|
+// NCPlayer.swift
|
|
|
// Nextcloud
|
|
|
//
|
|
|
// Created by Marino Faggiana on 01/07/21.
|
|
@@ -25,80 +25,66 @@ import Foundation
|
|
|
import NCCommunication
|
|
|
import UIKit
|
|
|
|
|
|
-class NCViewerVideo: NSObject {
|
|
|
+class NCPlayer: AVPlayer {
|
|
|
|
|
|
private let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
private var imageVideoContainer: imageVideoContainerView?
|
|
|
private var durationSeconds: Double = 0
|
|
|
- private var viewerVideoToolBar: NCViewerVideoToolBar?
|
|
|
+ private var playerToolBar: NCPlayerToolBar?
|
|
|
|
|
|
public var metadata: tableMetadata?
|
|
|
public var videoLayer: AVPlayerLayer?
|
|
|
- public var player: AVPlayer?
|
|
|
|
|
|
//MARK: - NotificationCenter
|
|
|
|
|
|
@objc func applicationDidEnterBackground(_ notification: NSNotification) {
|
|
|
|
|
|
if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
|
|
|
- self.player?.pause()
|
|
|
+ self.pause()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- init(viewerVideoToolBar: NCViewerVideoToolBar?, metadata: tableMetadata) {
|
|
|
- super.init()
|
|
|
-
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
+ deinit {
|
|
|
+ print("deinit NCPlayer")
|
|
|
+ }
|
|
|
+
|
|
|
+ func setupVideoLayer(imageVideoContainer: imageVideoContainerView?, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata) {
|
|
|
+
|
|
|
+ self.playerToolBar = playerToolBar
|
|
|
+ self.metadata = metadata
|
|
|
|
|
|
- func initPlayer(url: URL) {
|
|
|
-
|
|
|
- self.player = AVPlayer(url: url)
|
|
|
- self.player?.isMuted = CCUtility.getAudioMute()
|
|
|
- self.player?.seek(to: .zero)
|
|
|
+ isMuted = CCUtility.getAudioMute()
|
|
|
+ seek(to: .zero)
|
|
|
|
|
|
- // At end go back to start & show toolbar
|
|
|
- NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { (notification) in
|
|
|
- if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
|
|
|
- self.player?.seek(to: .zero)
|
|
|
- self.viewerVideoToolBar?.showToolBar(metadata: metadata)
|
|
|
- NCKTVHTTPCache.shared.saveCache(metadata: metadata)
|
|
|
- }
|
|
|
+ // At end go back to start & show toolbar
|
|
|
+ NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: currentItem, queue: .main) { (notification) in
|
|
|
+ if let item = notification.object as? AVPlayerItem, let currentItem = self.currentItem, item == currentItem {
|
|
|
+ self.seek(to: .zero)
|
|
|
+ self.playerToolBar?.showToolBar(metadata: metadata)
|
|
|
+ NCKTVHTTPCache.shared.saveCache(metadata: metadata)
|
|
|
}
|
|
|
-
|
|
|
- // save durationSeconds on database
|
|
|
- if let duration: CMTime = (player?.currentItem?.asset.duration) {
|
|
|
- durationSeconds = CMTimeGetSeconds(duration)
|
|
|
- NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationSeconds: durationSeconds)
|
|
|
- }
|
|
|
-
|
|
|
- // NO Live Photo, seek to datamebase time
|
|
|
- if !metadata.livePhoto, let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
|
|
|
- self.player?.seek(to: time)
|
|
|
- }
|
|
|
-
|
|
|
- viewerVideoToolBar?.setBarPlayer(viewerVideo: self)
|
|
|
}
|
|
|
|
|
|
- if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
|
|
|
-
|
|
|
- self.viewerVideoToolBar = viewerVideoToolBar
|
|
|
- self.metadata = metadata
|
|
|
-
|
|
|
- initPlayer(url: url)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- deinit {
|
|
|
- print("deinit NCViewerVideo")
|
|
|
- }
|
|
|
-
|
|
|
- func setupVideoLayer(imageVideoContainer: imageVideoContainerView?) {
|
|
|
+ // save durationSeconds on database
|
|
|
+ if let duration: CMTime = (currentItem?.asset.duration) {
|
|
|
+ durationSeconds = CMTimeGetSeconds(duration)
|
|
|
+ NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationSeconds: durationSeconds)
|
|
|
+ }
|
|
|
+
|
|
|
+ // NO Live Photo, seek to datamebase time
|
|
|
+ if !metadata.livePhoto, let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
|
|
|
+ seek(to: time)
|
|
|
+ }
|
|
|
+
|
|
|
+ playerToolBar?.setBarPlayer(player: self)
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
|
|
|
if let imageVideoContainer = imageVideoContainer {
|
|
|
|
|
|
self.imageVideoContainer = imageVideoContainer
|
|
|
|
|
|
- self.videoLayer = AVPlayerLayer(player: self.player)
|
|
|
+ self.videoLayer = AVPlayerLayer(player: self)
|
|
|
self.videoLayer!.frame = imageVideoContainer.bounds
|
|
|
self.videoLayer!.videoGravity = .resizeAspect
|
|
|
|
|
@@ -110,16 +96,14 @@ class NCViewerVideo: NSObject {
|
|
|
|
|
|
func videoPlay() {
|
|
|
|
|
|
- self.player?.play()
|
|
|
+ play()
|
|
|
}
|
|
|
|
|
|
func videoPause() {
|
|
|
guard let metadata = self.metadata else { return }
|
|
|
|
|
|
- self.player?.pause()
|
|
|
- if let time = self.player?.currentTime() {
|
|
|
- NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
|
|
|
- }
|
|
|
+ pause()
|
|
|
+ NCManageDatabase.shared.addVideoTime(metadata: metadata, time: currentTime(), durationSeconds: nil)
|
|
|
|
|
|
NCKTVHTTPCache.shared.saveCache(metadata: metadata)
|
|
|
}
|
|
@@ -127,7 +111,7 @@ class NCViewerVideo: NSObject {
|
|
|
func videoSeek(time: CMTime) {
|
|
|
guard let metadata = self.metadata else { return }
|
|
|
|
|
|
- self.player?.seek(to: time)
|
|
|
+ seek(to: time)
|
|
|
NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
|
|
|
}
|
|
|
|
|
@@ -140,10 +124,7 @@ class NCViewerVideo: NSObject {
|
|
|
|
|
|
func getVideoCurrentSeconds() -> Float64 {
|
|
|
|
|
|
- if let currentTime = player?.currentTime() {
|
|
|
- return CMTimeGetSeconds(currentTime)
|
|
|
- }
|
|
|
- return 0
|
|
|
+ return CMTimeGetSeconds(currentTime())
|
|
|
}
|
|
|
|
|
|
func getVideoDurationSeconds() -> Float64 {
|