Преглед на файлове

选择音乐cell增加loading功能

huzhiqiang преди 3 години
родител
ревизия
17ba0132f8

BIN
BFFramework/Assets/Stuckpoint/allPreview.png


BIN
BFFramework/Assets/Stuckpoint/loading@2x.png


BIN
BFFramework/Assets/Stuckpoint/loading@3x.png


BIN
BFFramework/Assets/Stuckpoint/musicEditerBtn_h.png


BIN
BFFramework/Assets/Stuckpoint/musicEditerBtn_n.png


BIN
BFFramework/Assets/Stuckpoint/stuckPoint_music_pause.png


BIN
BFFramework/Assets/Stuckpoint/stuckPoint_music_selected.png


+ 6 - 1
BFFramework/Classes/Stuckpoint/Controller/PQStuckPointMusicContentController.swift

@@ -248,8 +248,13 @@ extension PQStuckPointMusicContentController: UICollectionViewDelegate, UICollec
                     (itemList[indexPath.item] as? PQVoiceModel)?.isSelected = true
                     (itemList[indexPath.item] as? PQVoiceModel)?.isPlaying = true
                 }
+                var indexpaths = [indexPath]
+                if lastIndexPath != nil {
+                    indexpaths.append(lastIndexPath!)
+                }
+                collectionView.reloadItems(at: indexpaths)
                 lastIndexPath = indexPath
-                collectionView.reloadData()
+//                collectionView.reloadData()
             } else if contentType != .catagery {
                 (itemList[indexPath.item] as? PQVoiceModel)?.isPlaying = !((itemList[indexPath.item] as? PQVoiceModel)?.isPlaying ?? false)
                 (itemList[indexPath.item] as? PQVoiceModel)?.isSelected = true

+ 17 - 1
BFFramework/Classes/Stuckpoint/Controller/PQStuckPointMusicController.swift

@@ -42,7 +42,7 @@ class PQStuckPointMusicController: PQBaseViewController {
                             self.stuckPointEditVC?.firstFrameImage = image
                         }
                         
-                        BFLog(1, message: "aaa: \(Date().timeIntervalSince(startTime)), \(String(describing: image?.size))")
+//                        BFLog(1, message: "aaa: \(Date().timeIntervalSince(startTime)), \(String(describing: image?.size))")
                     }
                 }
             }
@@ -408,6 +408,7 @@ class PQStuckPointMusicController: PQBaseViewController {
         pageNum = 1
     }
 
+    var avPlayerTimeObserver:Any?
     /// 播放音乐
     /// - Parameter itemData: <#itemData description#>
     func playStuckPointMusic(itemData: PQVoiceModel?, isClearCurrentMusic: Bool = false) {
@@ -419,6 +420,10 @@ class PQStuckPointMusicController: PQBaseViewController {
             avPlayer.pause()
             playerItem?.removeObserver(self, forKeyPath: "status")
             playerItem?.removeObserver(self, forKeyPath: "error")
+            if avPlayerTimeObserver != nil {
+                avPlayer.removeTimeObserver(avPlayerTimeObserver as Any)
+            }
+            
             playerItem = AVPlayerItem(url: URL(string: itemData?.musicPath ?? "")!)
             if (itemData?.endTime ?? 0) > 0, (itemData?.endTime ?? 0) > (itemData?.startTime ?? 0) {
                 playerItem?.forwardPlaybackEndTime = CMTime(value: CMTimeValue((itemData?.endTime ?? 0) * playerTimescale), timescale: CMTimeScale(playerTimescale))
@@ -428,6 +433,17 @@ class PQStuckPointMusicController: PQBaseViewController {
             playerItem?.addObserver(self, forKeyPath: "error", options: .new, context: nil)
             avPlayer.seek(to: CMTime(value: CMTimeValue((itemData?.startTime ?? 0) * playerTimescale), timescale: CMTimeScale(playerTimescale)))
             avPlayer.play()
+            
+            avPlayerTimeObserver = avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 10), queue: DispatchQueue.global()) {[weak self] time in
+                if fabs(CMTimeGetSeconds(time) - (itemData?.startTime ?? 0)) > 0.1 {
+                    self?.avPlayer.removeTimeObserver(self?.avPlayerTimeObserver as Any)
+                    self?.avPlayerTimeObserver = nil
+                    // 停止cell loading动画
+                    PQNotification.post(name: NSNotification.Name(rawValue: "MusicContentCellIconLoadingAnimationStop"), object: nil)
+                }
+             //    进度监控
+//                BFLog(1, message: "\(Int(CMTimeGetSeconds((self?.playerItem?.asset.duration)!))),curr:\(CMTimeGetSeconds(time))")
+            }
 //            let player = TXVodPlayer()
 //            let config = TXVodPlayConfig()
 //            config.cacheFolderPath = videoCacheDirectory

+ 24 - 3
BFFramework/Classes/Stuckpoint/View/PQStuckPointMusicContentCell.swift

@@ -22,7 +22,7 @@ class PQStuckPointMusicContentCell: UICollectionViewCell {
 
     lazy var imageMaskView: UIView = {
         let imageMaskView = UIView()
-        imageMaskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
+        imageMaskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
         imageMaskView.addCorner(corner: 4)
         return imageMaskView
     }()
@@ -96,6 +96,11 @@ class PQStuckPointMusicContentCell: UICollectionViewCell {
         contentView.addSubview(remindView)
         contentView.addSubview(confirmContentView)
         confirmContentView.addSubview(confirmBtn)
+        PQNotification.addObserver(forName: Notification.Name(rawValue: "MusicContentCellIconLoadingAnimationStop"), object: nil, queue: .main) { [weak self] notice in
+            if !(self?.imageMaskView.isHidden ?? true){
+                self?.stopLoadingAnimation()
+            }
+        }
     }
 
     required init?(coder _: NSCoder) {
@@ -117,10 +122,13 @@ class PQStuckPointMusicContentCell: UICollectionViewCell {
             audioImageView.setNetImage(url: "\((bgmData as? PQVoiceModel)?.avatarUrl ?? "")", placeholder: UIImage().BF_Image(named: "videomk_music_default"))
             confirmContentView.isHidden = !((bgmData as? PQVoiceModel)?.isSelected ?? false)
             if (bgmData as? PQVoiceModel)?.isSelected ?? false {
+                imageMaskView.isHidden = false
                 playImageView.isHidden = false
                 if (bgmData as? PQVoiceModel)?.isPlaying ?? false {
-                    playImageView.image = nil
-                    playImageView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
+                    playImageView.image = UIImage().BF_Image(named: "loading")
+                    startLoadingAnimation()
+
+//                    playImageView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
                     musicNameLab.move()
                 } else {
                     playImageView.image = UIImage().BF_Image(named: "stuckPoint_music_pause")
@@ -226,4 +234,17 @@ class PQStuckPointMusicContentCell: UICollectionViewCell {
             btnClickHandle!(confirmBtn, bgmData)
         }
     }
+    
+    func startLoadingAnimation(){
+        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
+        rotateAnimation.toValue = Double.pi * 2
+        rotateAnimation.duration = 1
+        rotateAnimation.repeatCount = .infinity
+        playImageView.layer.add(rotateAnimation, forKey: nil)
+    }
+    
+    func stopLoadingAnimation(){
+        playImageView.layer.removeAllAnimations()
+        playImageView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
+    }
 }