Ver Fonte

Merge branch 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit

* 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit:
  1.修复暂停回退问题
  1.合并播放录音
  1.合并播放录音
harry há 3 anos atrás
pai
commit
323b9e6822

+ 37 - 25
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -522,6 +522,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     self?.recordBtn.isHidden = (self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE && (self?.isEndPlay ?? false)) ? false : (self?.isEndPlay ?? false)
                 }
                 sself.currentPlayRecordIndex = -3 // 刚录音完,不需要播放录音
+                BFLog(3, message: "重置播放index = \(sself.currentPlayRecordIndex)")
                 // 重置录制开始时间
                 sself.recordStartTime = 0
             }
@@ -1026,6 +1027,7 @@ public class BFRecordScreenController: BFBaseViewController {
             }
             isDragingProgressSlder = false
             currentPlayRecordIndex = -1
+            BFLog(3, message: "重置播放index = \(currentPlayRecordIndex)")
             hadPrepareToPlayRecord = false
             progressThumV.progress = jumpTime
 
@@ -1072,6 +1074,7 @@ public class BFRecordScreenController: BFBaseViewController {
         changeProgress(progress: sender.value)
         isDragingProgressSlder = false
         currentPlayRecordIndex = -1
+        BFLog(3, message: "重置播放index = \(currentPlayRecordIndex)")
         hadPrepareToPlayRecord = false
     }
 
@@ -1242,9 +1245,12 @@ public class BFRecordScreenController: BFBaseViewController {
             avplayerTimeObserver = recordPlayer?.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 1000), queue: DispatchQueue.global()) { [weak self] time in
                 BFLog(3, message: "当前播放---\(time),\(time.seconds)")
                 if CMTimeGetSeconds(self?.currenStartPlayTime ?? CMTime.zero) <= 0 {
+                    BFLog(message: "重新更新开始播放进度\(#function)-\((self?.currenStartPlayTime.seconds ?? 0))")
                     self?.currenStartPlayTime = time
                 }
-                periodicTimeObserver(CMTime(seconds: CMTimeGetSeconds(time) - CMTimeGetSeconds(self?.currenStartPlayTime ?? CMTime.zero), preferredTimescale: 1000), newItem)
+                let progressTime = CMTime(seconds: CMTimeGetSeconds(time) - CMTimeGetSeconds(self?.currenStartPlayTime ?? CMTime.zero), preferredTimescale: 1000)
+                BFLog(message: "progressTime进度\(#function)-\(progressTime.seconds)")
+                periodicTimeObserver(progressTime, newItem)
             } as? NSKeyValueObservation
         }
         
@@ -1285,7 +1291,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     }
                     currentPlayRecordIndex = shouldPlayRecordIndex
                     hadPrepareToPlayRecord = true
-                    BFLog(1, message: "录音开始播放2, \(second), \(CMTimeGetSeconds(recordPlayer?.currentItem?.duration ?? .zero))")
+                    BFLog(1, message: "录音开始播放2, \(second), \(CMTimeGetSeconds(recordPlayer?.currentItem?.duration ?? .zero)),index = \(currentPlayRecordIndex)")
                 }
             }
         }
@@ -1301,6 +1307,7 @@ public class BFRecordScreenController: BFBaseViewController {
             assetPlayer?.seek(to: CMTime.zero)
             progressThumV.progress = 0
             currentPlayRecordIndex = -1
+            BFLog(3, message: "重置播放index = \(currentPlayRecordIndex)")
             if itemModels[currItemModelIndex].mediaType == .VIDEO {
                 recordBtn.isHidden = false
             }
@@ -1315,7 +1322,7 @@ public class BFRecordScreenController: BFBaseViewController {
             assetPlayer?.play()
         } else {
             // 处理图片音频播放
-            imageRecordPlay()
+            playRecord(time: currentAssetProgress)
         }
 
         deleteRecordBtn.isHidden = true
@@ -1329,7 +1336,8 @@ public class BFRecordScreenController: BFBaseViewController {
     func pause() {
         BFLog(1, message: "暂停播放")
         isNormalPlaying = false
-
+        avplayerTimeObserver?.invalidate()
+        avplayerTimeObserver = nil
         subtitleBtn.isHidden = false
         soundSettingBtn.isHidden = false
         withDrawBtn.isHidden = false
@@ -1417,14 +1425,9 @@ public class BFRecordScreenController: BFBaseViewController {
             avplayerTimeObserver = assetPlayer?.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 1000), queue: DispatchQueue.global()) { [weak self] time in
                 // 进度监控
                 self?.periodicTimeObserver(item: item, time: time)
-
                 if self?.isNormalPlaying ?? false {
-                    // 播放对应的录音音频
-                    self?.playRecord(at: time, periodicTimeObserver: { currentT, currentItem in
-                        BFLog(message: "播放一段进度:\(currentT),\(currentItem)")
-                    }, didPlayToEndTime: { recordInfo, _ in
-                        BFLog(message: "播放一段结束:\(recordInfo?.1.endTime ?? 0)")
-                    }, playFailed: { _, _ in })
+                    // 播放录音
+                    self?.playRecord(time: time)
                 }
             } as? NSKeyValueObservation
         }
@@ -1455,6 +1458,7 @@ public class BFRecordScreenController: BFBaseViewController {
                 currentAssetProgress = CMTime(seconds: time.seconds, preferredTimescale: 1000)
                 BFLog(1, message: "curr:\(CMTimeGetSeconds(currentAssetProgress))")
                 DispatchQueue.main.async { [weak self] in
+                    BFLog(message: "更新录音进度\(#function)-\((self?.currentAssetProgress.seconds ?? 0))")
                     self?.progreddL.text = String(format: "%@", CMTimeGetSeconds(time).formatDurationToHMS())
                     let su = !(self?.isDragingProgressSlder ?? false) || (self?.isRecording ?? false) || (self?.isNormalPlaying ?? false)
                     if su {
@@ -1506,6 +1510,7 @@ public class BFRecordScreenController: BFBaseViewController {
             if let duration = assetPlayer?.currentItem?.duration {
                 currentAssetProgress = CMTime(value: CMTimeValue(newProgress * Float(CMTimeGetSeconds(duration)) * 1000), timescale: 1000)
                 DispatchQueue.main.async { [weak self] in
+                    BFLog(message: "更新录音进度\(#function)-\((self?.currentAssetProgress.seconds ?? 0))")
                     self!.progreddL.text = String(format: "%@", CMTimeGetSeconds(self!.currentAssetProgress).formatDurationToHMS())
                 }
                 assetPlayer!.seek(to: currentAssetProgress, toleranceBefore: CMTime(value: 1, timescale: 1_000_000), toleranceAfter: CMTime(value: 1, timescale: 1_000_000)) { _ in
@@ -1514,6 +1519,7 @@ public class BFRecordScreenController: BFBaseViewController {
         } else {
             currentAssetProgress = isBack ? CMTime(value: CMTimeValue(progress * 1000), timescale: 1000) : CMTime(value: CMTimeValue(progress * Float(itemModels[currItemModelIndex].materialDuraion) * 1000), timescale: 1000)
             DispatchQueue.main.async { [weak self] in
+                BFLog(message: "更新录音进度\(#function)-\((self?.currentAssetProgress.seconds ?? 0))")
                 self?.progreddL.text = String(format: "%@", CMTimeGetSeconds(self!.currentAssetProgress).formatDurationToHMS())
             }
             BFLog(message: "progress = \(progress),currentAssetProgress = \(currentAssetProgress.seconds),materialDuraion = \(itemModels[currItemModelIndex].materialDuraion)")
@@ -1568,6 +1574,7 @@ extension BFRecordScreenController: GPUImageMovieDelegate {
     public func didCompletePlayingMovie() {
         BFLog(1, message: "播放结束")
         currentPlayRecordIndex = -1
+        BFLog(3, message: "重置播放index = \(currentPlayRecordIndex)")
     }
 }
 
@@ -1695,8 +1702,11 @@ extension BFRecordScreenController: UICollectionViewDelegate, UICollectionViewDa
 
 /// 处理图片素材
 public extension BFRecordScreenController {
-    /// 处理图片音频的播放
-    func imageRecordPlay() {
+    
+    /// 播放录音
+    /// - Parameter time: <#time description#>
+    func playRecord(time:CMTime) {
+        // 播放对应的录音音频
         if itemModels[currItemModelIndex].mediaType == .IMAGE {
             if itemModels[currItemModelIndex].materialDuraion <= 0 || currentAssetProgress.seconds >= itemModels[currItemModelIndex].materialDuraion {
                 (collectionView.cellForItem(at: IndexPath(item: currItemModelIndex, section: 0)) as? BFImageCoverViewCell)?.playBtn.isSelected = false
@@ -1706,11 +1716,15 @@ public extension BFRecordScreenController {
             // 当开始播放时重置录音播放起始时间
             recordStartPlayTime = currentAssetProgress
             currenStartPlayTime = CMTime.zero
-            playRecord(at: currentAssetProgress, periodicTimeObserver: { [weak self] currentT, currentItem in
-                BFLog(1, message: "播放图片素材进度:\(currentT),\(currentItem)")
+        }
+        playRecord(at: time, periodicTimeObserver: { [weak self] currentT, currentItem in
+            BFLog(1, message: "播放录音进度:\(currentT),\(currentItem)")
+            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE && (self?.isNormalPlaying ?? false) {
                 self?.imageRecordProgress(progress: CMTimeGetSeconds(currentT))
-            }, didPlayToEndTime: { [weak self] recordInfo, currentItem in
-                BFLog(message: "播放图片素材录音结束:\(String(describing: recordInfo?.1)),\(String(describing: currentItem))")
+            }
+        }, didPlayToEndTime: { [weak self] recordInfo, currentItem in
+            BFLog(message: "播放录音结束:\(String(describing: recordInfo?.1)),\(String(describing: currentItem))")
+            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE {
                 if (self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.count ?? 0) <= ((recordInfo?.0 ?? 0) + 1) || (recordInfo?.1.endTime ?? 0) >= (self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last?.endTime ?? 0) {
                     self?.isEndPlay = true
                     self?.pause()
@@ -1719,19 +1733,16 @@ public extension BFRecordScreenController {
                     self?.resetCurrentProgress()
                     // 录制播放结束后显示录制按钮
                     self?.recordBtn.isHidden = false
-                    // 跳转下一段
-//                    if (self?.currItemModelIndex ?? 0) < ((self?.itemModels.count ?? 0) - 1) {
-//                        self?.collectionView.setContentOffset(CGPoint(x: CGFloat((self?.currItemModelIndex ?? 0) + 1) * (self?.collectionView.frame.width ?? 0), y: 0), animated: true)
-//                    }
-
                 } else {
                     // 注:矫正进度--一段录音播放结束后当前指针应该到当前录音结束点
                     self?.currentAssetProgress = CMTime(seconds: self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers[(recordInfo?.0 ?? 0) + 1].startTime ?? 0, preferredTimescale: 1000)
-                    self?.imageRecordPlay()
+                    self?.playRecord(time: self?.currentAssetProgress ?? CMTime.zero)
                 }
-            }) { [weak self] _, _ in
+            }
+        }) { [weak self] _, _ in
+            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE  {
                 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
-                    self?.imageRecordPlay()
+                    self?.playRecord(time: self?.currentAssetProgress ?? CMTime.zero)
                 }
             }
         }
@@ -1754,6 +1765,7 @@ public extension BFRecordScreenController {
     /// 重置进度
     func resetCurrentProgress() {
         DispatchQueue.main.async { [weak self] in
+            BFLog(message: "更新录音进度\(#function)-\((self?.currentAssetProgress.seconds ?? 0))")
             self?.progreddL.text = String(format: "%@", (self?.currentAssetProgress.seconds ?? 0).formatDurationToHMS())
             self?.progressThumV.progress = (self?.currentAssetProgress.seconds ?? 0)
             self?.updateSubtitle(time: self?.currentAssetProgress ?? CMTime.zero)