|
@@ -22,9 +22,9 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
// MARK: - 录制参数
|
|
// MARK: - 录制参数
|
|
public var asset:PHAsset?
|
|
public var asset:PHAsset?
|
|
// var shouldPlayRecordIndex:Int = -1 // 当前应该播放的录音资源序号
|
|
// var shouldPlayRecordIndex:Int = -1 // 当前应该播放的录音资源序号
|
|
- var currentPlayRecordIndex:Int = -1 // 当前正在播放的录音资源序号
|
|
|
|
|
|
+ var currentPlayRecordIndex:Int = -1 // >= 0 :当前正在播放的录音资源序号; -3: 刚录音完,不需要播放录音; -1:初始化阶段
|
|
var isRecording = false // 是否正在录音
|
|
var isRecording = false // 是否正在录音
|
|
- var isNormalPlaying = false { // 是否正在录音
|
|
|
|
|
|
+ var isNormalPlaying = false { // 是否正在播放
|
|
didSet{
|
|
didSet{
|
|
playBtn.isSelected = isNormalPlaying
|
|
playBtn.isSelected = isNormalPlaying
|
|
}
|
|
}
|
|
@@ -52,7 +52,7 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
// 录音相关
|
|
// 录音相关
|
|
lazy var recorderManager : BFRecordManager = {
|
|
lazy var recorderManager : BFRecordManager = {
|
|
|
|
|
|
- let manager = BFRecordManager(voideModeL: PQVoiceModel())
|
|
|
|
|
|
+ let manager = BFRecordManager()
|
|
manager.cancelRecordHandle = { error in
|
|
manager.cancelRecordHandle = { error in
|
|
|
|
|
|
}
|
|
}
|
|
@@ -62,6 +62,8 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
let ass = AVURLAsset(url: URL(fileURLWithPath: model.wavFilePath))
|
|
let ass = AVURLAsset(url: URL(fileURLWithPath: model.wavFilePath))
|
|
|
|
|
|
model.endTime = model.startTime + CMTimeGetSeconds(ass.duration)
|
|
model.endTime = model.startTime + CMTimeGetSeconds(ass.duration)
|
|
|
|
+
|
|
|
|
+ // TODO: 原逻辑要删除新录音后边的数据, 新逻辑是插入覆盖
|
|
while let m = self?.recordList.last{
|
|
while let m = self?.recordList.last{
|
|
if model.startTime < m.startTime {
|
|
if model.startTime < m.startTime {
|
|
self?.recordList.removeLast()
|
|
self?.recordList.removeLast()
|
|
@@ -74,6 +76,7 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
BFLog(1, message: "添加录音文件:\(model.startTime) -- \(model.endTime)")
|
|
BFLog(1, message: "添加录音文件:\(model.startTime) -- \(model.endTime)")
|
|
self?.recordList.append(model)
|
|
self?.recordList.append(model)
|
|
self?.drawOrUpdateRecordProgessLable()
|
|
self?.drawOrUpdateRecordProgessLable()
|
|
|
|
+ self?.currentPlayRecordIndex = -3 // 刚录音完,不需要播放录音
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -309,7 +312,8 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
|
|
|
|
let model = PQVoiceModel()
|
|
let model = PQVoiceModel()
|
|
model.startTime = CMTimeGetSeconds(self.currentAssetProgress)
|
|
model.startTime = CMTimeGetSeconds(self.currentAssetProgress)
|
|
- recorderManager.voiceModel? = model
|
|
|
|
|
|
+ model.volume = 100
|
|
|
|
+ recorderManager.voiceModel = model
|
|
recorderManager.startRecord(index: recordList.count)
|
|
recorderManager.startRecord(index: recordList.count)
|
|
movie?.startProcessing()
|
|
movie?.startProcessing()
|
|
assetPlayer?.volume = 0
|
|
assetPlayer?.volume = 0
|
|
@@ -355,7 +359,7 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
changeProgress(progress: sender.value)
|
|
changeProgress(progress: sender.value)
|
|
isDragingProgressSlder = false
|
|
isDragingProgressSlder = false
|
|
currentPlayRecordIndex = -1
|
|
currentPlayRecordIndex = -1
|
|
-
|
|
|
|
|
|
+ hadPrepareToPlayRecord = false
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - 权限申请
|
|
// MARK: - 权限申请
|
|
@@ -405,6 +409,9 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
|
|
|
|
// MARK: - 音视频处理
|
|
// MARK: - 音视频处理
|
|
func playRecord(at currentT:CMTime){
|
|
func playRecord(at currentT:CMTime){
|
|
|
|
+ if currentPlayRecordIndex == -3 { // 刚录音完,不需要播放
|
|
|
|
+ return
|
|
|
|
+ }
|
|
let (shouldPlayRecordIndex, recordedAudio) = recordList.enumerated().first { model in
|
|
let (shouldPlayRecordIndex, recordedAudio) = recordList.enumerated().first { model in
|
|
model.1.endTime > CMTimeGetSeconds(currentT)
|
|
model.1.endTime > CMTimeGetSeconds(currentT)
|
|
} ?? (-1, nil)
|
|
} ?? (-1, nil)
|
|
@@ -417,13 +424,26 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
|
|
|
|
// 创建播放器
|
|
// 创建播放器
|
|
if self.recordPlayer == nil || (self.recordPlayer?.currentItem?.asset as? AVURLAsset)?.url.lastPathComponent != (recordedAudio.wavFilePath as NSString).lastPathComponent {
|
|
if self.recordPlayer == nil || (self.recordPlayer?.currentItem?.asset as? AVURLAsset)?.url.lastPathComponent != (recordedAudio.wavFilePath as NSString).lastPathComponent {
|
|
- self.recordPlayer?.pause()
|
|
|
|
- self.recordPlayer = AVPlayer(url: URL(fileURLWithPath: recordedAudio.wavFilePath))
|
|
|
|
|
|
+ let newItem = AVPlayerItem(url: URL(fileURLWithPath: recordedAudio.wavFilePath))
|
|
|
|
+ if let player = self.recordPlayer {
|
|
|
|
+ player.pause()
|
|
|
|
+ if let playItem = player.currentItem {
|
|
|
|
+ NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: playItem)
|
|
|
|
+ recordPlayer?.replaceCurrentItem(with: newItem)
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ self.recordPlayer = AVPlayer(playerItem: newItem)
|
|
|
|
+ }
|
|
self.recordPlayer!.volume = 1
|
|
self.recordPlayer!.volume = 1
|
|
// self.recordPlayer?.prepareToPlay()
|
|
// self.recordPlayer?.prepareToPlay()
|
|
currentPlayRecordIndex = -1
|
|
currentPlayRecordIndex = -1
|
|
hadPrepareToPlayRecord = false
|
|
hadPrepareToPlayRecord = false
|
|
BFLog(1, message: "录音播放器初始化(有时候不准)")
|
|
BFLog(1, message: "录音播放器初始化(有时候不准)")
|
|
|
|
+
|
|
|
|
+ NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: newItem, queue: .main) { [weak self] notify in
|
|
|
|
+ self?.hadPrepareToPlayRecord = false
|
|
|
|
+ self?.currentPlayRecordIndex = -1
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
synced(currentPlayRecordIndex) {
|
|
synced(currentPlayRecordIndex) {
|
|
@@ -433,15 +453,17 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
{
|
|
{
|
|
// 应当开始播放了
|
|
// 应当开始播放了
|
|
// 两个逻辑:如果在播,则跳过;如果暂停拖动到中间,则seek
|
|
// 两个逻辑:如果在播,则跳过;如果暂停拖动到中间,则seek
|
|
- if currentPlayRecordIndex == -1 {
|
|
|
|
|
|
+ if currentPlayRecordIndex == -1 && self.isNormalPlaying{
|
|
let second = CMTimeGetSeconds(currentT) - recordedAudio.startTime
|
|
let second = CMTimeGetSeconds(currentT) - recordedAudio.startTime
|
|
- recordPlayer?.seek(to: CMTime(value: CMTimeValue(second), timescale: 100), toleranceBefore: CMTime(value: 1, timescale: 1000), toleranceAfter: CMTime(value: 1, timescale: 1000), completionHandler: {[weak self] finished in
|
|
|
|
- if finished && (self?.isNormalPlaying ?? false) {
|
|
|
|
- DispatchQueue.main.async {[weak self] in
|
|
|
|
|
|
+ DispatchQueue.main.async {[weak self] in
|
|
|
|
+ self?.recordPlayer?.seek(to: CMTime(value: CMTimeValue(second), timescale: 100), toleranceBefore: CMTime(value: 1, timescale: 1000), toleranceAfter: CMTime(value: 1, timescale: 1000), completionHandler: {[weak self] finished in
|
|
|
|
+ if finished && (self?.isNormalPlaying ?? false) {
|
|
self?.recordPlayer?.play()
|
|
self?.recordPlayer?.play()
|
|
|
|
+ BFLog(1, message: "录音开始播放 playing, \(second), \(CMTimeGetSeconds(self?.recordPlayer?.currentItem?.duration ?? .zero))")
|
|
|
|
+
|
|
}
|
|
}
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ })
|
|
|
|
+ }
|
|
currentPlayRecordIndex = shouldPlayRecordIndex
|
|
currentPlayRecordIndex = shouldPlayRecordIndex
|
|
hadPrepareToPlayRecord = true
|
|
hadPrepareToPlayRecord = true
|
|
BFLog(1, message: "录音开始播放2, \(second), \(CMTimeGetSeconds(recordPlayer?.currentItem?.duration ?? .zero))")
|
|
BFLog(1, message: "录音开始播放2, \(second), \(CMTimeGetSeconds(recordPlayer?.currentItem?.duration ?? .zero))")
|
|
@@ -596,9 +618,9 @@ public class BFRecordScreenController: BFBaseViewController {
|
|
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: assetPlayer?.currentItem, queue: .main) { [weak self] notify in
|
|
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: assetPlayer?.currentItem, queue: .main) { [weak self] notify in
|
|
- BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
|
|
|
|
|
|
+ BFLog(1, message: "AVPlayerItemDidPlayToEndTime = \(notify)")
|
|
|
|
+ self?.isNormalPlaying = false
|
|
self?.assetPlayer?.seek(to: CMTime.zero)
|
|
self?.assetPlayer?.seek(to: CMTime.zero)
|
|
- self?.playBtn.isSelected = false
|
|
|
|
self?.currentPlayRecordIndex = -1
|
|
self?.currentPlayRecordIndex = -1
|
|
}
|
|
}
|
|
}
|
|
}
|