|
@@ -24,452 +24,273 @@
|
|
|
import Foundation
|
|
|
import NextcloudKit
|
|
|
import UIKit
|
|
|
-import AVFoundation
|
|
|
-import MediaPlayer
|
|
|
-import JGProgressHUD
|
|
|
-import Alamofire
|
|
|
+import MobileVLCKit
|
|
|
|
|
|
class NCPlayer: NSObject {
|
|
|
|
|
|
internal let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
- internal var url: URL
|
|
|
+ internal var url: URL?
|
|
|
+ internal var player: VLCMediaPlayer?
|
|
|
+ internal var thumbnailer: VLCMediaThumbnailer?
|
|
|
+ internal var metadata: tableMetadata
|
|
|
+ internal var singleTapGestureRecognizer: UITapGestureRecognizer!
|
|
|
+ internal var width: Int64?
|
|
|
+ internal var height: Int64?
|
|
|
+ internal let fileNamePreviewLocalPath: String
|
|
|
+ internal let fileNameIconLocalPath: String
|
|
|
+
|
|
|
internal weak var playerToolBar: NCPlayerToolBar?
|
|
|
- internal weak var viewController: UIViewController?
|
|
|
- internal var autoPlay: Bool
|
|
|
- internal var isProxy: Bool
|
|
|
- internal var isStartPlayer: Bool
|
|
|
- internal var isStartObserver: Bool
|
|
|
- internal var subtitleUrls: [URL] = []
|
|
|
- internal var currentSubtitle: URL?
|
|
|
-
|
|
|
- private weak var imageVideoContainer: imageVideoContainerView?
|
|
|
- private weak var detailView: NCViewerMediaDetailView?
|
|
|
- private var observerAVPlayerItemDidPlayToEndTime: Any?
|
|
|
- private var observerAVPlayertTime: Any?
|
|
|
-
|
|
|
- var kvoPlayerObserver: NSKeyValueObservation?
|
|
|
- var player: AVPlayer?
|
|
|
- var durationTime: CMTime = .zero
|
|
|
- var metadata: tableMetadata
|
|
|
- var videoLayer: AVPlayerLayer?
|
|
|
+ internal weak var viewerMediaPage: NCViewerMediaPage?
|
|
|
+
|
|
|
+ weak var imageVideoContainer: imageVideoContainerView?
|
|
|
|
|
|
// MARK: - View Life Cycle
|
|
|
|
|
|
- init(url: URL, autoPlay: Bool, isProxy: Bool, imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?, viewController: UIViewController) {
|
|
|
+ init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, viewerMediaPage: NCViewerMediaPage?) {
|
|
|
|
|
|
- self.url = url
|
|
|
- self.autoPlay = autoPlay
|
|
|
- self.isProxy = isProxy
|
|
|
- self.isStartPlayer = false
|
|
|
- self.isStartObserver = false
|
|
|
self.imageVideoContainer = imageVideoContainer
|
|
|
self.playerToolBar = playerToolBar
|
|
|
self.metadata = metadata
|
|
|
- self.detailView = detailView
|
|
|
- self.viewController = viewController
|
|
|
+ self.viewerMediaPage = viewerMediaPage
|
|
|
|
|
|
- super.init()
|
|
|
+ fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
|
|
|
+ fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
|
|
|
|
|
|
- do {
|
|
|
- try AVAudioSession.sharedInstance().setCategory(.playback)
|
|
|
- try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.none)
|
|
|
- try AVAudioSession.sharedInstance().setActive(true)
|
|
|
- } catch {
|
|
|
- print(error)
|
|
|
- }
|
|
|
+ super.init()
|
|
|
}
|
|
|
|
|
|
deinit {
|
|
|
|
|
|
print("deinit NCPlayer with ocId \(metadata.ocId)")
|
|
|
- deactivateObserver()
|
|
|
+ NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
}
|
|
|
|
|
|
- func openAVPlayer() {
|
|
|
+ func openAVPlayer(url: URL) {
|
|
|
|
|
|
-#if MFFFLIB
|
|
|
- MFFF.shared.setDelegate = self
|
|
|
- MFFF.shared.dismissMessage()
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(convertVideoDidFinish(_:)), name: NSNotification.Name(rawValue: self.metadata.ocId), object: nil)
|
|
|
+ let userAgent = CCUtility.getUserAgent()!
|
|
|
+ var position: Float = 0
|
|
|
|
|
|
- if CCUtility.fileProviderStorageExists(metadata) {
|
|
|
- self.url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: NCGlobal.shared.fileNameVideoEncoded))
|
|
|
- self.isProxy = false
|
|
|
- }
|
|
|
- if MFFF.shared.existsMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))) {
|
|
|
- return
|
|
|
- }
|
|
|
-#endif
|
|
|
-
|
|
|
- // Check already started
|
|
|
- if isStartPlayer {
|
|
|
- if !isStartObserver {
|
|
|
- print("Play already started - URL: \(self.url)")
|
|
|
- activateObserver()
|
|
|
- playerToolBar?.show()
|
|
|
- }
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- print("Play URL: \(self.url)")
|
|
|
- player = AVPlayer(url: self.url)
|
|
|
- playerToolBar?.show()
|
|
|
- playerToolBar?.setMetadata(self.metadata)
|
|
|
-#if MFFFLIB
|
|
|
- setUpForSubtitle()
|
|
|
-#endif
|
|
|
-
|
|
|
- if metadata.livePhoto {
|
|
|
- player?.isMuted = false
|
|
|
- } else if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
|
|
|
- player?.isMuted = CCUtility.getAudioMute()
|
|
|
- } else {
|
|
|
- player?.isMuted = CCUtility.getAudioMute()
|
|
|
- if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
|
|
|
- player?.seek(to: time)
|
|
|
- }
|
|
|
- }
|
|
|
+ self.url = url
|
|
|
+ self.singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
|
|
|
|
|
|
- let observerAVPlayertStatus = self.player?.currentItem?.observe(\.status, options: [.new,.initial]) { player, change in
|
|
|
+ print("Play URL: \(url)")
|
|
|
+ player = VLCMediaPlayer()
|
|
|
+ player?.media = VLCMedia(url: url)
|
|
|
+ player?.delegate = self
|
|
|
|
|
|
- if let player = self.player,
|
|
|
- let playerItem = player.currentItem,
|
|
|
- let object = player.currentItem,
|
|
|
- playerItem === object,
|
|
|
- self.viewController != nil {
|
|
|
+ // player?.media?.addOption("--network-caching=500")
|
|
|
+ player?.media?.addOption(":http-user-agent=\(userAgent)")
|
|
|
|
|
|
- if self.isStartPlayer {
|
|
|
- return
|
|
|
- }
|
|
|
- if (playerItem.status == .readyToPlay || playerItem.status == .failed) {
|
|
|
- print("Player ready")
|
|
|
- self.startPlayer()
|
|
|
- } else {
|
|
|
- print("Player not ready")
|
|
|
- }
|
|
|
- }
|
|
|
+ if let result = NCManageDatabase.shared.getVideoPosition(metadata: metadata) {
|
|
|
+ position = result
|
|
|
+ player?.position = position
|
|
|
}
|
|
|
|
|
|
- if let observerAVPlayertStatus = observerAVPlayertStatus{
|
|
|
- kvoPlayerObserver = observerAVPlayertStatus
|
|
|
+ player?.drawable = imageVideoContainer
|
|
|
+ if let view = player?.drawable as? UIView {
|
|
|
+ view.isUserInteractionEnabled = true
|
|
|
+ view.addGestureRecognizer(singleTapGestureRecognizer)
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- func startPlayer() {
|
|
|
|
|
|
- player?.currentItem?.asset.loadValuesAsynchronously(forKeys: ["playable"], completionHandler: {
|
|
|
+ playerToolBar?.setBarPlayer(ncplayer: self, position: position, metadata: metadata)
|
|
|
|
|
|
- var error: NSError? = nil
|
|
|
- let status = self.player?.currentItem?.asset.statusOfValue(forKey: "playable", error: &error) ?? .unknown
|
|
|
-
|
|
|
- DispatchQueue.main.async {
|
|
|
-
|
|
|
- switch status {
|
|
|
- case .loaded:
|
|
|
-
|
|
|
- self.durationTime = self.player?.currentItem?.asset.duration ?? .zero
|
|
|
- NCManageDatabase.shared.addVideoTime(metadata: self.metadata, time: nil, durationTime: self.durationTime)
|
|
|
-
|
|
|
- self.videoLayer = AVPlayerLayer(player: self.player)
|
|
|
- self.videoLayer!.frame = self.imageVideoContainer?.bounds ?? .zero
|
|
|
- self.videoLayer!.videoGravity = .resizeAspect
|
|
|
+ if let media = player?.media {
|
|
|
+ thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)
|
|
|
+ }
|
|
|
|
|
|
- if self.metadata.classFile == NKCommon.TypeClassFile.video.rawValue {
|
|
|
- self.imageVideoContainer?.layer.addSublayer(self.videoLayer!)
|
|
|
- self.imageVideoContainer?.playerLayer = self.videoLayer
|
|
|
- self.imageVideoContainer?.metadata = self.metadata
|
|
|
- self.imageVideoContainer?.image = self.imageVideoContainer?.image?.image(alpha: 0)
|
|
|
- }
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
+ }
|
|
|
|
|
|
- self.playerToolBar?.setBarPlayer(ncplayer: self)
|
|
|
- self.generatorImagePreview()
|
|
|
- if !(self.detailView?.isShow() ?? false) {
|
|
|
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":self.metadata.ocId, "enableTimerAutoHide": false])
|
|
|
- }
|
|
|
- self.activateObserver()
|
|
|
- if self.autoPlay || CCUtility.getPlayerPlay() {
|
|
|
- self.player?.play()
|
|
|
- }
|
|
|
- self.isStartPlayer = true
|
|
|
- break
|
|
|
-
|
|
|
- case .failed:
|
|
|
-
|
|
|
- self.playerToolBar?.hide()
|
|
|
- if self.isProxy && NCKTVHTTPCache.shared.getDownloadStatusCode(metadata: self.metadata) == 200 {
|
|
|
- let alertController = UIAlertController(title: NSLocalizedString("_error_", value: "Error", comment: ""), message: NSLocalizedString("_video_not_streamed_", comment: ""), preferredStyle: .alert)
|
|
|
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", value: "Yes", comment: ""), style: .default, handler: { _ in
|
|
|
- self.downloadVideo()
|
|
|
- }))
|
|
|
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", value: "No", comment: ""), style: .default, handler: { _ in }))
|
|
|
- self.viewController?.present(alertController, animated: true)
|
|
|
- } else if self.metadata.isDirectoryE2EE {
|
|
|
- let alertController = UIAlertController(title: NSLocalizedString("_info_", value: "Info", comment: ""), message: NSLocalizedString("_video_not_streamed_e2ee_", comment: ""), preferredStyle: .alert)
|
|
|
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", value: "Yes", comment: ""), style: .default, handler: { _ in
|
|
|
- self.downloadVideo(isEncrypted: true)
|
|
|
- }))
|
|
|
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", value: "No", comment: ""), style: .default, handler: { _ in }))
|
|
|
- self.viewController?.present(alertController, animated: true)
|
|
|
- } else {
|
|
|
-#if MFFFLIB
|
|
|
- if error?.code == AVError.Code.fileFormatNotRecognized.rawValue {
|
|
|
- self.convertVideo(withAlert: true)
|
|
|
- break
|
|
|
- }
|
|
|
-#endif
|
|
|
- if let title = error?.localizedDescription, let description = error?.localizedFailureReason {
|
|
|
- let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: description)
|
|
|
- NCContentPresenter.shared.messageNotification(title, error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
|
|
|
- } else {
|
|
|
- let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
|
|
|
- NCContentPresenter.shared.showError(error: error, priority: .max)
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
+ // MARK: - UIGestureRecognizerDelegate
|
|
|
|
|
|
- case .cancelled:
|
|
|
- break
|
|
|
+ @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
|
|
|
|
|
|
- default:
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
+ viewerMediaPage?.didSingleTapWith(gestureRecognizer: gestureRecognizer)
|
|
|
}
|
|
|
|
|
|
- func activateObserver() {
|
|
|
- print("activating Observer ocId \(metadata.ocId)")
|
|
|
+ // MARK: - NotificationCenter
|
|
|
|
|
|
- observerAVPlayerItemDidPlayToEndTime = NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { [weak self] notification in
|
|
|
+ @objc func applicationDidEnterBackground(_ notification: NSNotification) {
|
|
|
|
|
|
- guard let self = self else {
|
|
|
- return
|
|
|
- }
|
|
|
+ if metadata.classFile == NKCommon.TypeClassFile.video.rawValue {
|
|
|
+ playerStop()
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
|
|
|
+ // MARK: -
|
|
|
|
|
|
- NCKTVHTTPCache.shared.saveCache(metadata: self.metadata)
|
|
|
+ func isPlay() -> Bool {
|
|
|
|
|
|
- self.videoSeek(time: .zero)
|
|
|
+ return player?.isPlaying ?? false
|
|
|
+ }
|
|
|
|
|
|
- if !(self.detailView?.isShow() ?? false) {
|
|
|
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
|
|
|
- }
|
|
|
+ func playerPlay() {
|
|
|
|
|
|
- self.playerToolBar?.updateToolBar()
|
|
|
- }
|
|
|
+ playerToolBar?.playbackSliderEvent = .began
|
|
|
+ player?.play()
|
|
|
+ playerToolBar?.playButtonPause()
|
|
|
+
|
|
|
+ if let position = NCManageDatabase.shared.getVideoPosition(metadata: metadata) {
|
|
|
+ player?.position = position
|
|
|
+ playerToolBar?.playbackSliderEvent = .moved
|
|
|
}
|
|
|
|
|
|
- // Evey 1 second update toolbar
|
|
|
- observerAVPlayertTime = player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { [weak self] _ in
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
+ self.playerToolBar?.playbackSliderEvent = .ended
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- guard let self = self else {
|
|
|
- return
|
|
|
- }
|
|
|
+ @objc func playerStop() {
|
|
|
|
|
|
- if self.player?.currentItem?.status == .readyToPlay {
|
|
|
- self.playerToolBar?.updateToolBar()
|
|
|
- }
|
|
|
- })
|
|
|
+ savePosition()
|
|
|
+ player?.stop()
|
|
|
+ playerToolBar?.playButtonPlay()
|
|
|
+ }
|
|
|
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(generatorImagePreview), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
|
|
|
+ @objc func playerPause() {
|
|
|
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(playerPause), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(playerPlay), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
|
|
|
+ savePosition()
|
|
|
+ player?.pause()
|
|
|
+ playerToolBar?.playButtonPlay()
|
|
|
+ }
|
|
|
|
|
|
- if let player = self.player {
|
|
|
- NotificationCenter.default.addObserver(self, selector: #selector(playerStalled), name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: player.currentItem)
|
|
|
- }
|
|
|
+ func playerPosition(_ position: Float) {
|
|
|
|
|
|
- isStartObserver = true
|
|
|
+ NCManageDatabase.shared.addVideo(metadata: metadata, position: position)
|
|
|
+ player?.position = position
|
|
|
}
|
|
|
|
|
|
- func deactivateObserver() {
|
|
|
+ func savePosition() {
|
|
|
|
|
|
- print("deactivating Observer ocId \(metadata.ocId)")
|
|
|
+ guard let position = player?.position, metadata.classFile == NKCommon.TypeClassFile.video.rawValue, isPlay() else { return }
|
|
|
|
|
|
- if isPlay() {
|
|
|
- playerPause()
|
|
|
+ if let width = width, let height = height {
|
|
|
+ player?.saveVideoSnapshot(at: fileNamePreviewLocalPath, withWidth: Int32(width), andHeight: Int32(height))
|
|
|
}
|
|
|
-
|
|
|
- self.kvoPlayerObserver?.invalidate()
|
|
|
- self.kvoPlayerObserver = nil
|
|
|
|
|
|
- if let observerAVPlayerItemDidPlayToEndTime = self.observerAVPlayerItemDidPlayToEndTime {
|
|
|
- NotificationCenter.default.removeObserver(observerAVPlayerItemDidPlayToEndTime)
|
|
|
- }
|
|
|
- observerAVPlayerItemDidPlayToEndTime = nil
|
|
|
+ NCManageDatabase.shared.addVideo(metadata: metadata, position: position)
|
|
|
+ }
|
|
|
|
|
|
- if let observerAVPlayertTime = self.observerAVPlayertTime,
|
|
|
- let player = player {
|
|
|
- player.removeTimeObserver(observerAVPlayertTime)
|
|
|
- }
|
|
|
- observerAVPlayertTime = nil
|
|
|
+ func setVolumeAudio(_ volume: Int32) {
|
|
|
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
|
|
|
+ player?.audio?.volume = volume
|
|
|
+ }
|
|
|
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: nil)
|
|
|
+ func jumpForward(_ seconds: Int32) {
|
|
|
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
|
|
|
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
|
|
|
-
|
|
|
- isStartObserver = false
|
|
|
+ player?.jumpForward(seconds)
|
|
|
}
|
|
|
|
|
|
- // MARK: - NotificationCenter
|
|
|
+ func jumpBackward(_ seconds: Int32) {
|
|
|
|
|
|
- @objc func applicationDidEnterBackground(_ notification: NSNotification) {
|
|
|
+ player?.jumpBackward(seconds)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- if metadata.classFile == NKCommon.TypeClassFile.video.rawValue, let playerToolBar = self.playerToolBar {
|
|
|
- if !playerToolBar.isPictureInPictureActive() {
|
|
|
- playerPause()
|
|
|
+extension NCPlayer: VLCMediaPlayerDelegate {
|
|
|
+
|
|
|
+ func mediaPlayerStateChanged(_ aNotification: Notification) {
|
|
|
+ guard let player = self.player else { return }
|
|
|
+
|
|
|
+ switch player.state {
|
|
|
+ case .stopped:
|
|
|
+ print("Played mode: STOPPED")
|
|
|
+ break
|
|
|
+ case .opening:
|
|
|
+ print("Played mode: OPENING")
|
|
|
+ break
|
|
|
+ case .buffering:
|
|
|
+ print("Played mode: BUFFERING")
|
|
|
+ break
|
|
|
+ case .ended:
|
|
|
+ if let url = self.url {
|
|
|
+ NCManageDatabase.shared.addVideo(metadata: metadata, position: 0)
|
|
|
+ NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
|
|
|
+ self.thumbnailer?.fetchThumbnail()
|
|
|
+ self.openAVPlayer(url: url)
|
|
|
}
|
|
|
+ print("Played mode: ENDED")
|
|
|
+ break
|
|
|
+ case .error:
|
|
|
+ playerToolBar?.disableAllControl()
|
|
|
+ let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
|
|
|
+ NCContentPresenter.shared.showError(error: error, priority: .max)
|
|
|
+ print("Played mode: ERROR")
|
|
|
+ break
|
|
|
+ case .playing:
|
|
|
+ if let tracksInformation = player.media?.tracksInformation {
|
|
|
+ for case let track as [String:Any] in tracksInformation {
|
|
|
+ if track["type"] as? String == "video" {
|
|
|
+ width = track["width"] as? Int64
|
|
|
+ height = track["height"] as? Int64
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ print("Played mode: PLAYING")
|
|
|
+ break
|
|
|
+ case .paused:
|
|
|
+ print("Played mode: PAUSED")
|
|
|
+ break
|
|
|
+ default: break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @objc func applicationDidBecomeActive(_ notification: NSNotification) {
|
|
|
+ func mediaPlayerTimeChanged(_ aNotification: Notification) {
|
|
|
|
|
|
- playerToolBar?.updateToolBar()
|
|
|
+ playerToolBar?.update()
|
|
|
}
|
|
|
|
|
|
- // MARK: -
|
|
|
-
|
|
|
- func isPlay() -> Bool {
|
|
|
-
|
|
|
- if player?.rate == 1 { return true } else { return false }
|
|
|
+ func mediaPlayerTitleChanged(_ aNotification: Notification) {
|
|
|
+ // Handle other states...
|
|
|
}
|
|
|
|
|
|
- @objc func playerStalled() {
|
|
|
-
|
|
|
- print("current player \(String(describing: player)) stalled.\nCalling playerPlay()")
|
|
|
- playerPlay()
|
|
|
+ func mediaPlayerChapterChanged(_ aNotification: Notification) {
|
|
|
+ // Handle other states...
|
|
|
}
|
|
|
|
|
|
- @objc func playerPlay() {
|
|
|
-
|
|
|
- player?.play()
|
|
|
- playerToolBar?.updateToolBar()
|
|
|
+ func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
|
|
|
+ // Handle other states...
|
|
|
}
|
|
|
|
|
|
- @objc func playerPause() {
|
|
|
-
|
|
|
- player?.pause()
|
|
|
- playerToolBar?.updateToolBar()
|
|
|
-
|
|
|
- if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
|
|
|
- playerToolBar.pictureInPictureController?.stopPictureInPicture()
|
|
|
+ func mediaPlayerSnapshot(_ aNotification: Notification) {
|
|
|
+
|
|
|
+ if let data = NSData(contentsOfFile: fileNamePreviewLocalPath),
|
|
|
+ let image = UIImage(data: data as Data),
|
|
|
+ let image = image.resizeImage(size: CGSize(width: NCGlobal.shared.sizeIcon, height: NCGlobal.shared.sizeIcon)),
|
|
|
+ let data = image.jpegData(compressionQuality: 0.5) {
|
|
|
+ try? data.write(to: URL(fileURLWithPath: fileNameIconLocalPath))
|
|
|
}
|
|
|
+ print("Snapshot saved on \(fileNameIconLocalPath)")
|
|
|
}
|
|
|
|
|
|
- func videoSeek(time: CMTime) {
|
|
|
-
|
|
|
- player?.seek(to: time)
|
|
|
- saveTime(time)
|
|
|
+ func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
|
|
|
+ // Handle other states...
|
|
|
}
|
|
|
|
|
|
- func saveTime(_ time: CMTime) {
|
|
|
-
|
|
|
- if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue { return }
|
|
|
-
|
|
|
- NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationTime: nil)
|
|
|
- generatorImagePreview()
|
|
|
+ func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
|
|
|
+ // Handle other states...
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- func saveCurrentTime() {
|
|
|
+extension NCPlayer: VLCMediaThumbnailerDelegate {
|
|
|
|
|
|
- if let player = self.player {
|
|
|
- saveTime(player.currentTime())
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @objc func generatorImagePreview() {
|
|
|
+ func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer) { }
|
|
|
|
|
|
- guard let time = player?.currentTime(), !metadata.livePhoto, metadata.classFile != NKCommon.TypeClassFile.audio.rawValue else { return }
|
|
|
+ func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer, didFinishThumbnail thumbnail: CGImage) {
|
|
|
|
|
|
var image: UIImage?
|
|
|
|
|
|
- if let asset = player?.currentItem?.asset {
|
|
|
-
|
|
|
- do {
|
|
|
- let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
|
|
|
- let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
|
|
|
- let imageGenerator = AVAssetImageGenerator(asset: asset)
|
|
|
- imageGenerator.appliesPreferredTrackTransform = true
|
|
|
- let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
|
|
|
- image = UIImage(cgImage: cgImage)
|
|
|
- // Update Playing Info Center
|
|
|
- let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
|
|
|
- if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
|
|
|
- MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
|
|
|
- return image
|
|
|
- }
|
|
|
- }
|
|
|
- // Preview
|
|
|
- if let data = image?.jpegData(compressionQuality: 0.5) {
|
|
|
- try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
|
|
|
- }
|
|
|
- // Icon
|
|
|
- if let data = image?.jpegData(compressionQuality: 0.5) {
|
|
|
- try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
|
|
|
- }
|
|
|
- } catch let error as NSError {
|
|
|
- print("GeneratorImagePreview localized error:")
|
|
|
- print(error.localizedDescription)
|
|
|
+ do {
|
|
|
+ image = UIImage(cgImage: thumbnail)
|
|
|
+ if let data = image?.jpegData(compressionQuality: 0.5) {
|
|
|
+ try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- internal func downloadVideo(isEncrypted: Bool = false, requiredConvert: Bool = false) {
|
|
|
-
|
|
|
- guard let view = appDelegate.window?.rootViewController?.view else { return }
|
|
|
- let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
|
|
|
- let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
|
|
|
- let hud = JGProgressHUD()
|
|
|
- var downloadRequest: DownloadRequest?
|
|
|
-
|
|
|
- hud.indicatorView = JGProgressHUDRingIndicatorView()
|
|
|
- if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
|
|
|
- indicatorView.ringWidth = 1.5
|
|
|
- }
|
|
|
- hud.textLabel.text = NSLocalizedString(metadata.fileNameView, comment: "")
|
|
|
- hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
|
|
|
- hud.show(in: view)
|
|
|
- hud.tapOnHUDViewBlock = { hud in
|
|
|
- downloadRequest?.cancel()
|
|
|
- }
|
|
|
-
|
|
|
- NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
|
|
|
- downloadRequest = request
|
|
|
- } taskHandler: { task in
|
|
|
- // task
|
|
|
- } progressHandler: { progress in
|
|
|
- hud.progress = Float(progress.fractionCompleted)
|
|
|
- } completionHandler: { _, _, _, _, _, afError, error in
|
|
|
- if afError == nil {
|
|
|
- NCManageDatabase.shared.addLocalFile(metadata: self.metadata)
|
|
|
- if isEncrypted {
|
|
|
- if let result = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "fileNameIdentifier == %@ AND serverUrl == %@", self.metadata.fileName, self.metadata.serverUrl)) {
|
|
|
- NCEndToEndEncryption.sharedManager()?.decryptFile(self.metadata.fileName, fileNameView: self.metadata.fileNameView, ocId: self.metadata.ocId, key: result.key, initializationVector: result.initializationVector, authenticationTag: result.authenticationTag)
|
|
|
- }
|
|
|
- }
|
|
|
- let urlVideo = NCKTVHTTPCache.shared.getVideoURL(metadata: self.metadata)
|
|
|
- if let url = urlVideo.url {
|
|
|
- self.url = url
|
|
|
- self.isProxy = urlVideo.isProxy
|
|
|
- if requiredConvert {
|
|
|
-#if MFFFLIB
|
|
|
- self.convertVideo(withAlert: false)
|
|
|
-#endif
|
|
|
- } else {
|
|
|
- self.openAVPlayer()
|
|
|
- }
|
|
|
- }
|
|
|
+ if let data = image?.jpegData(compressionQuality: 0.5) {
|
|
|
+ try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
|
|
|
}
|
|
|
- hud.dismiss()
|
|
|
+ } catch let error as NSError {
|
|
|
+ print("GeneratorImagePreview localized error:")
|
|
|
+ print(error.localizedDescription)
|
|
|
}
|
|
|
}
|
|
|
}
|