|
@@ -70,32 +70,16 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
|
|
|
guard let wself = self else { return }
|
|
|
|
|
|
wself.currentAssetProgress = CMTime(seconds: (currTime + wself.currPlayTime).seconds, preferredTimescale: 1000)
|
|
|
+ wself.dele?.currentAssetProgress = wself.currentAssetProgress
|
|
|
// BFLog(1, message: "camera currTime: \(currTime.seconds)")
|
|
|
DispatchQueue.main.async {[weak self] in
|
|
|
guard let wself = self else { return }
|
|
|
-
|
|
|
+ wself.dele?.updateSubtitle(time: wself.currentAssetProgress)
|
|
|
wself.locationTo(time: wself.currentAssetProgress)
|
|
|
}
|
|
|
|
|
|
}) as? NSKeyValueObservation
|
|
|
|
|
|
- NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: p.currentItem, queue: .main) { [weak self] _ in
|
|
|
- guard let wself = self else {
|
|
|
- return
|
|
|
- }
|
|
|
- BFLog(1, message: "endPlay:\(wself.currentAssetProgress.seconds), 总时长:\(wself.recordItem?.materialDuraion.seconds)")
|
|
|
-
|
|
|
- // 如果离结束大于0.7s,代表还有下一段视频
|
|
|
- if ((wself.recordItem?.materialDuraion ?? .zero) - wself.currentAssetProgress).seconds > 0.7 {
|
|
|
- // 直接加33ms下一帧的时间去找视频播放,因为结束比实际时长要早一点点
|
|
|
- wself.currentAssetProgress = wself.currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)
|
|
|
- wself.play()
|
|
|
-
|
|
|
- }else{
|
|
|
- // 播放到末尾了
|
|
|
- wself.dele?.pause()
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
return p
|
|
|
}()
|
|
@@ -231,9 +215,15 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
|
|
|
let dur = AVURLAsset(url: URL(fileURLWithPath: finalPath))
|
|
|
wself.videoModel.timelineCMOut = wself.videoModel.timelineCMIn + CMTime(seconds: dur.duration.seconds, preferredTimescale: 1000)
|
|
|
BFLog(1, message: "拍摄文件时长:\(wself.videoModel.timelineCMIn.seconds)~\(wself.videoModel.timelineCMOut.seconds), \(dur.duration.seconds)")
|
|
|
- wself.videoModel.locationPath = finalPath
|
|
|
- wself.currentAssetProgress = wself.videoModel.timelineCMOut
|
|
|
- wself.recordEndCallBack?(true, wself.videoModel)
|
|
|
+ if (wself.videoModel.timelineCMOut - wself.videoModel.timelineCMIn).seconds > 1 {
|
|
|
+ wself.videoModel.locationPath = finalPath
|
|
|
+ wself.currentAssetProgress = wself.videoModel.timelineCMOut
|
|
|
+ wself.recordEndCallBack?(true, wself.videoModel)
|
|
|
+ }else{
|
|
|
+ wself.revertLast()
|
|
|
+ BFLog(1, message: "摄像录制小于1s")
|
|
|
+ wself.recordEndCallBack?(false, nil)
|
|
|
+ }
|
|
|
|
|
|
}else{
|
|
|
// 录制失败后恢复原样
|
|
@@ -289,45 +279,30 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
|
|
|
currentAssetProgress = .zero
|
|
|
currPlayTime = .zero
|
|
|
locationTo(time: currentAssetProgress)
|
|
|
-
|
|
|
+ avplayer.replaceCurrentItem(with: nil)
|
|
|
}
|
|
|
|
|
|
- if assetPlayer?.currentItem != nil {
|
|
|
- assetPlayer?.play()
|
|
|
+ if avplayer.currentItem != nil {
|
|
|
+ avplayer.play()
|
|
|
recordPlayer?.play()
|
|
|
}else{
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 播放视频
|
|
|
- if let mod = recordItem?.videoStickers.first(where: { m in
|
|
|
- CMTimeCompare(m.timelineCMIn, currentAssetProgress) <= 0 && CMTimeCompare(m.timelineCMOut, currentAssetProgress) > 0
|
|
|
- }){
|
|
|
- avplayer.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: mod.locationPath)))
|
|
|
- currPlayTime = mod.timelineCMIn
|
|
|
+ prepareToPlayNext()
|
|
|
|
|
|
- let tt = currentAssetProgress - currPlayTime
|
|
|
- BFLog(1, message: "asset - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds)")
|
|
|
- avplayer.seek(to: tt, toleranceBefore: CMTime(seconds: 1, preferredTimescale: 1000), toleranceAfter: CMTime(seconds: 1, preferredTimescale: 1000)) { [weak avplayer] isFinished in
|
|
|
- if isFinished {
|
|
|
- avplayer?.play()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- playRecordVoice(tt)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- func playRecordVoice(_ tt:CMTime){
|
|
|
+ func playRecordVoice(_ tt:CMTime, needPlay:Bool = true){
|
|
|
// 播放音频
|
|
|
if let mod = recordItem?.voiceStickers.first(where: { m in
|
|
|
- CMTimeCompare(m.startCMTime, currentAssetProgress) <= 0 && CMTimeCompare(m.endCMTime, currentAssetProgress) > 0
|
|
|
+ CMTimeCompare(m.startCMTime, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(m.endCMTime, currentAssetProgress) > 0
|
|
|
}){
|
|
|
recordPlayer?.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: mod.wavFilePath)))
|
|
|
|
|
|
BFLog(1, message: "record - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds)")
|
|
|
recordPlayer?.seek(to: tt, toleranceBefore: CMTime(seconds: 1, preferredTimescale: 1000), toleranceAfter: CMTime(seconds: 1, preferredTimescale: 1000)) { [weak recordPlayer] isFinished in
|
|
|
- if isFinished {
|
|
|
+ if isFinished && needPlay{
|
|
|
recordPlayer?.play()
|
|
|
}
|
|
|
}
|
|
@@ -414,6 +389,60 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ override func dragEnd() {
|
|
|
+ // 播放视频
|
|
|
+ if let mod = recordItem?.videoStickers.first(where: { m in
|
|
|
+ CMTimeCompare(m.timelineCMIn, currentAssetProgress) <= 0 && CMTimeCompare(m.timelineCMOut, currentAssetProgress) > 0
|
|
|
+ }){
|
|
|
+ avplayerReplaceItem(newItem: AVPlayerItem(url: URL(fileURLWithPath: mod.locationPath)))
|
|
|
+ currPlayTime = mod.timelineCMIn
|
|
|
+
|
|
|
+ let tt = currentAssetProgress - currPlayTime
|
|
|
+ BFLog(1, message: "asset - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds)")
|
|
|
+ avplayer.seek(to: tt, toleranceBefore: CMTime(seconds: 1, preferredTimescale: 1000), toleranceAfter: CMTime(seconds: 1, preferredTimescale: 1000))
|
|
|
+
|
|
|
+ playRecordVoice(tt, needPlay: false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func prepareToPlayNext(){
|
|
|
+ if let mod = recordItem?.videoStickers.sorted(by: { m1, m2 in
|
|
|
+ m1.timelineCMIn.seconds < m2.timelineCMIn.seconds
|
|
|
+ }).first(where: { mod in
|
|
|
+ CMTimeCompare(mod.timelineCMIn, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(currentAssetProgress, mod.timelineCMOut) < 0
|
|
|
+ }){
|
|
|
+ currPlayTime = mod.timelineCMIn
|
|
|
+ locationTo(time: mod.timelineCMIn)
|
|
|
+ avplayerReplaceItem(newItem: AVPlayerItem(url: URL(fileURLWithPath: mod.locationPath)))
|
|
|
+ avplayer.play()
|
|
|
+ playRecordVoice(.zero)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func avplayerReplaceItem(newItem: AVPlayerItem){
|
|
|
+ if let item = avplayer.currentItem {
|
|
|
+ NotificationCenter.default.removeObserver(self as Any, name: .AVPlayerItemDidPlayToEndTime, object: item)
|
|
|
+ }
|
|
|
+
|
|
|
+ avplayer.replaceCurrentItem(with: newItem)
|
|
|
+ NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avplayer.currentItem, queue: .main) { [weak self] notice in
|
|
|
+ guard let wself = self else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ BFLog(1, message: "endPlay:\(wself.currentAssetProgress.seconds), 总时长:\(wself.recordItem!.materialDuraion.seconds), currplayer:\(wself.avplayer.currentItem?.currentTime().seconds)")
|
|
|
+
|
|
|
+ // 如果离结束大于0.7s,代表还有下一段视频
|
|
|
+ if ((wself.recordItem?.materialDuraion ?? .zero) - wself.currentAssetProgress).seconds > 0.7 {
|
|
|
+ wself.prepareToPlayNext()
|
|
|
+ }else{
|
|
|
+ // 播放到末尾了
|
|
|
+ wself.dele?.isEndPlay = true
|
|
|
+ wself.dele?.pause()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
override func clean() {
|
|
|
filter.removeTarget(movieWrite)
|
|
|
filter.removeTarget(rendView)
|