Преглед изворни кода

Audio improvements

Signed-off-by: marinofaggiana <marino@marinofaggiana.com>
marinofaggiana пре 3 година
родитељ
комит
8090f0caf2

+ 5 - 1
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayer.swift

@@ -46,7 +46,7 @@ class NCPlayer: NSObject {
     public var videoLayer: AVPlayerLayer?
     public var pictureInPictureController: AVPictureInPictureController?
     
-    init(url: URL, imageVideoContainer: imageVideoContainerView?, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?) {
+    init(url: URL, autoPlay: Bool, imageVideoContainer: imageVideoContainerView?, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?) {
         super.init()
 
         var timeSeek: CMTime = .zero
@@ -131,6 +131,10 @@ class NCPlayer: NSObject {
                     self.durationTime = self.player?.currentItem?.asset.duration ?? .zero
                     NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationTime: self.durationTime)
                     
+                    if autoPlay {
+                        self.player?.play()
+                    }
+                    
                     self.playerToolBar?.setBarPlayer(ncplayer: self, timeSeek: timeSeek, metadata: metadata, image: imageVideoContainer?.image)
                     self.generatorImagePreview()
                     if !(detailView?.isShow() ?? false) {

+ 32 - 9
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayerToolBar.swift

@@ -254,7 +254,7 @@ class NCPlayerToolBar: UIView {
             return .commandFailed
         }
         
-        // VIDEO
+        // VIDEO () ()
         if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
             
             skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
@@ -272,19 +272,42 @@ class NCPlayerToolBar: UIView {
             }
         }
                 
-        // AUDIO
+        // AUDIO < >
         if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
             
-            nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { event in
+            if let currentIndex = self.viewerMedia?.currentIndex, let metadatas = self.viewerMedia?.metadatas {
+                            
+                var index: Int = 0
                 
-                self.viewerMedia?.goTo(.forward)
-                return.success
-            }
+                if currentIndex == metadatas.count - 1 {
+                    index = 0
+                } else {
+                    index = currentIndex + 1
+                }
+                    
+                if metadatas[index].classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
+                            
+                    nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { event in
+                        
+                        self.viewerMedia?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
+                        return.success
+                    }
+                }
             
-            previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand.addTarget { event in
+                if currentIndex == 0 {
+                    index = metadatas.count - 1
+                } else {
+                    index = currentIndex - 1
+                }
                 
-                self.viewerMedia?.goTo(.reverse)
-                return.success
+                if metadatas[index].classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
+                    
+                    previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand.addTarget { event in
+                        
+                        self.viewerMedia?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
+                        return.success
+                    }
+                }
             }
         }
         

+ 6 - 18
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -431,26 +431,16 @@ extension NCViewerMedia: UIPageViewControllerDelegate, UIPageViewControllerDataS
         return true
     }
     
-    func goTo(_ direction: UIPageViewController.NavigationDirection) {
+    func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
         
-        if direction == .forward {
-            if currentIndex == metadatas.count - 1 {
-                currentIndex = 0
-            } else {
-                currentIndex += 1
-            }
-        } else if direction == .forward && currentIndex == 0 {
-            if currentIndex == 0 {
-                currentIndex = 0
-            } else {
-                currentIndex -= 1
-            }
-        }
+        currentIndex = index
         
         if let viewerMediaZoom = getCache(index: currentIndex) {
+            viewerMediaZoom.autoPlay = autoPlay
             pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
         } else {
             let viewerMediaZoom = setCache(index: currentIndex, image: getImageMetadata(metadatas[currentIndex]), metadata: metadatas[currentIndex], direction: direction)
+            viewerMediaZoom.autoPlay = autoPlay
             pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
         }
     }
@@ -584,8 +574,7 @@ extension NCViewerMedia: UIGestureRecognizerDelegate {
                     AudioServicesPlaySystemSound(1519) // peek feedback
                     
                     if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
-                        self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
-                        self.ncplayerLivePhoto?.playerPlay()
+                        self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
                     }
                     
                 } else {
@@ -613,8 +602,7 @@ extension NCViewerMedia: UIGestureRecognizerDelegate {
                                 AudioServicesPlaySystemSound(1519) // peek feedback
                                 
                                 if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
-                                    self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
-                                    self.ncplayerLivePhoto?.playerPlay()
+                                    self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
                                 }
                             }
                         }

+ 15 - 2
iOSClient/Viewer/NCViewerMedia/NCViewerMediaZoom.swift

@@ -37,6 +37,8 @@ class NCViewerMediaZoom: UIViewController {
     @IBOutlet weak var detailView: NCViewerMediaDetailView!
     @IBOutlet weak var playerToolBar: NCPlayerToolBar!
     
+    private var _autoPlay: Bool = false
+
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     var viewerMedia: NCViewerMedia?
     var ncplayer: NCPlayer?
@@ -46,7 +48,18 @@ class NCViewerMediaZoom: UIViewController {
     var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
     var imageViewConstraint: CGFloat = 0
     var isDetailViewInitializze: Bool = false
-                
+    
+    var autoPlay: Bool {
+        get {
+            let temp = _autoPlay
+            _autoPlay = false
+            return temp
+        }
+        set(newVal) {
+            _autoPlay = newVal
+        }
+    }
+    
     // MARK: - View Life Cycle
 
     required init?(coder aDecoder: NSCoder) {
@@ -144,7 +157,7 @@ class NCViewerMediaZoom: UIViewController {
         
         if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
             if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
-                self.ncplayer = NCPlayer.init(url: url, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView)
+                self.ncplayer = NCPlayer.init(url: url, autoPlay: autoPlay, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView)
             }
         }