Pārlūkot izejas kodu

1.修复暂停回退问题

wenweiwei 3 gadi atpakaļ
vecāks
revīzija
0d47239aa5

+ 17 - 5
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
             }
@@ -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
@@ -1450,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 {
@@ -1501,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
@@ -1509,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)")
@@ -1563,6 +1574,7 @@ extension BFRecordScreenController: GPUImageMovieDelegate {
     public func didCompletePlayingMovie() {
         BFLog(1, message: "播放结束")
         currentPlayRecordIndex = -1
+        BFLog(3, message: "重置播放index = \(currentPlayRecordIndex)")
     }
 }
 
@@ -1707,7 +1719,7 @@ public extension BFRecordScreenController {
         }
         playRecord(at: time, periodicTimeObserver: { [weak self] currentT, currentItem in
             BFLog(1, message: "播放录音进度:\(currentT),\(currentItem)")
-            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE {
+            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE && (self?.isNormalPlaying ?? false) {
                 self?.imageRecordProgress(progress: CMTimeGetSeconds(currentT))
             }
         }, didPlayToEndTime: { [weak self] recordInfo, currentItem in
@@ -1753,7 +1765,7 @@ public extension BFRecordScreenController {
     /// 重置进度
     func resetCurrentProgress() {
         DispatchQueue.main.async { [weak self] in
-            BFLog(message: "更新\((self?.currentAssetProgress.seconds ?? 0))")
+            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)