Parcourir la source

视频播放 -- 自动循环

Chris il y a 5 ans
Parent
commit
d77d977f54

+ 2 - 0
RainbowPlanet/RainbowPlanet/Modules/CommunityModule/CommunityVideoContent/View/CommunityVideoCoverCollectionCell.swift

@@ -58,6 +58,7 @@ class CommunityVideoCoverCollectionCell: UICollectionViewCell {
         addSubview(videoPlayView)
         
         addSubview(delButton)
+        
     }
     
     private func setupLayouts() {
@@ -98,6 +99,7 @@ class CommunityVideoCoverCollectionCell: UICollectionViewCell {
         return delButton
     }()
     
+    // MARK: -
     func play() {
         videoPlayView.play()
     }

+ 27 - 6
RainbowPlanet/RainbowPlanet/Modules/CommunityModule/CommunityVideoContent/View/CommunityVideoPlayView.swift

@@ -31,15 +31,22 @@ class CommunityVideoPlayView: UIView {
     private var player: AVPlayer?
 //    private var indicatorView: UIActivityIndicatorView?
     
-    init() {
-        super.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
-        initSubView()
+    deinit {
+        if observe != nil {
+            NotificationCenter.default.removeObserver(observe!)
+        }
     }
+    weak var observe : NSObjectProtocol?
     
-    override init(frame: CGRect) {
-        super.init(frame: frame)
+    init() {
+        super.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
         initSubView()
-    }
+        
+        observe = NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: playerItem, queue: OperationQueue.main, using: {
+            [weak self] (notification) in
+            self?.playerItemDidReachEnd(notification)
+        })
+    }    
     
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
@@ -68,6 +75,20 @@ class CommunityVideoPlayView: UIView {
         player?.pause()
     }
     
+    // 自动重播
+    func playerItemDidReachEnd(_ notification: Notification?) {
+        let item = notification?.object as? AVPlayerItem
+        if playerItem == item {
+            weak var weakSelf = self
+            DispatchQueue.main.async(execute: {
+                self.pause()
+                weakSelf?.player?.seek(to: .zero, toleranceBefore: .zero, toleranceAfter: .zero, completionHandler: { finished in
+                    weakSelf?.play()
+                })
+            })
+        }
+    }
+    
     
     
 //    func setupViews() {

+ 56 - 0
RainbowPlanet/RainbowPlanet/Modules/CommunityModule/CommunityVideoContent/ViewController/CommunityVideoListController.swift

@@ -16,11 +16,37 @@ class CommunityVideoListController: BaseViewController {
     
     var videoItemList: Array<CommunityVideoItemModel>?
     
+    private var prePlayCell: CommunityVideoCoverCollectionCell?
+    
+    deinit {
+        if observe != nil {
+            NotificationCenter.default.removeObserver(observe!)
+        }
+    }
+    weak var observe : NSObjectProtocol?
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         setupViews()
         setupData()
+        setUpAppStatusNotification()
+    }
+    
+    func setUpAppStatusNotification() {
+        observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main, using: {
+            [weak self] (notification) in
+            self?.pauseCurrentVideo()
+        })
         
+        observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
+            [weak self] (notification) in
+            self?.startPlay(self?.prePlayCell)
+        })
+        
+        observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: {
+            [weak self] (notification) in
+            self?.startPlay(self?.prePlayCell)
+        })
     }
     
     override func setupViews() {        
@@ -106,4 +132,34 @@ extension CommunityVideoListController: UICollectionViewDelegateFlowLayout,UICol
         return CGSize(width: kScreenWidth, height: 10)
     }
     
+    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
+        prePlayCell = cell as? CommunityVideoCoverCollectionCell
+        self.pauseCurrentVideo()
+    }
+    
+    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
+        let index = Int(collectionView.contentOffset.y / CGFloat(kScreenHeight))
+        let indexPath = IndexPath(item: index, section: 0)
+        let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
+        
+        if(cell != nil) {
+            self.startPlay(cell)
+        }
+    }
+    
+}
+
+// MARK: -
+extension CommunityVideoListController {
+    
+    func startPlay(_ cell: CommunityVideoCoverCollectionCell?) {
+        cell?.play()
+    }
+    
+    func pauseCurrentVideo() {
+        if prePlayCell != nil {
+            prePlayCell?.pause()
+        }
+    }
+    
 }