|
@@ -4,7 +4,7 @@
|
|
|
//
|
|
|
// Created by ak on 2022/3/4.
|
|
|
// 功能:音乐裁剪界面,设置开始播放位置
|
|
|
-
|
|
|
+
|
|
|
import BFCommonKit
|
|
|
import BFMediaKit
|
|
|
import BFUIKit
|
|
@@ -15,30 +15,32 @@ import UIKit
|
|
|
public enum MusiceCutActionType: Int {
|
|
|
case MusiceCutActionCancel = 1 // 取消
|
|
|
case MusiceCutActionConfirm = 2 // 确认
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
+class BFMusicCutView: UIView, UIGestureRecognizerDelegate {
|
|
|
var waveLayers: [CAShapeLayer] = Array<CAShapeLayer>.init()
|
|
|
// 裁剪时间回调
|
|
|
var cutTimeHandle: ((_ startTime: Float64, _ endTime: Float64, _ bgmData: PQVoiceModel?) -> Void)?
|
|
|
-
|
|
|
+
|
|
|
let normalMargin: CGFloat = cDefaultMargin * 2
|
|
|
-
|
|
|
- //记录设置的起点, 在点击确认后会设置 bgmdata 的 开始时间为本值。
|
|
|
+
|
|
|
+ // 记录设置的起点, 在点击确认后会设置 bgmdata 的 开始时间为本值。
|
|
|
var startCMTime: CMTime = .zero // 开始时间
|
|
|
-
|
|
|
- //播放音乐
|
|
|
+
|
|
|
+ // 播放音乐
|
|
|
lazy var avPlayer: AVPlayer = {
|
|
|
let avPlayer = AVPlayer()
|
|
|
PQNotification.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: .main) { [weak self] notify in
|
|
|
BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
|
|
|
guard let wself = self else { return }
|
|
|
-
|
|
|
- wself.avPlayer.seek(to: CMTime.init(value: CMTimeValue((wself.bgmData?.currentTime ?? 0.0)) * Int64(playerTimescale), timescale: CMTimeScale(playerTimescale)))
|
|
|
- wself.resetWave()
|
|
|
|
|
|
- wself.avPlayer.play()
|
|
|
+ if(!wself.isHidden){
|
|
|
+ wself.avPlayer.seek(to: CMTime(value: CMTimeValue((wself.bgmData?.currentTime ?? 0.0)) * Int64(playerTimescale), timescale: CMTimeScale(playerTimescale)))
|
|
|
+ wself.resetWave()
|
|
|
+
|
|
|
+ wself.avPlayer.play()
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
|
|
|
BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
|
|
@@ -51,15 +53,14 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
}
|
|
|
avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] _ in
|
|
|
guard let wself = self else { return }
|
|
|
-
|
|
|
+
|
|
|
let currentTime = CMTimeGetSeconds(avPlayer.currentItem?.currentTime() ?? CMTime.zero)
|
|
|
wself.configPlayProgress(currentTime: currentTime)
|
|
|
}
|
|
|
return avPlayer
|
|
|
}()
|
|
|
-
|
|
|
-
|
|
|
- //放指针的背景图
|
|
|
+
|
|
|
+ // 放指针的背景图
|
|
|
lazy var panCutBackView: UIView = {
|
|
|
let panCutBackView = UIView()
|
|
|
panCutBackView.backgroundColor = .clear
|
|
@@ -68,7 +69,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
return panCutBackView
|
|
|
}()
|
|
|
|
|
|
- //指针
|
|
|
+ // 指针
|
|
|
lazy var cutRemindView: UIView = {
|
|
|
let cutRemindView = UIView()
|
|
|
cutRemindView.backgroundColor = UIColor.hexColor(hexadecimal: "#389AFF")
|
|
@@ -76,15 +77,15 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
cutRemindView.frame = CGRect(x: (panCutBackView.frame.width - 2) / 2, y: 0, width: 2, height: panCutBackView.frame.height)
|
|
|
return cutRemindView
|
|
|
}()
|
|
|
-
|
|
|
- //水波纹的背景
|
|
|
+
|
|
|
+ // 水波纹的背景
|
|
|
lazy var progressImage: UIImageView = {
|
|
|
let progressImage = UIImageView()
|
|
|
progressImage.frame = CGRect(x: 20, y: currentProgressLab.frame.maxY + 2, width: cScreenWidth - 20 * 2, height: cDefaultMargin * 6)
|
|
|
return progressImage
|
|
|
}()
|
|
|
-
|
|
|
- //显示进度
|
|
|
+
|
|
|
+ // 显示进度
|
|
|
lazy var currentProgressLab: UILabel = {
|
|
|
let currentProgressLab = UILabel(frame: CGRect(x: cDefaultMargin * 3, y: cDefaultMargin * 6, width: cDefaultMargin * 6, height: cDefaultMargin * 2))
|
|
|
currentProgressLab.textColor = UIColor.hexColor(hexadecimal: "#616161")
|
|
@@ -95,43 +96,29 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
|
|
|
// 操作板背景
|
|
|
let backView = UIButton()
|
|
|
-
|
|
|
+
|
|
|
// 当前选择的音乐
|
|
|
public var bgmData: PQVoiceModel? {
|
|
|
didSet {
|
|
|
- BFLog(message: "设置的音乐开始时间currentTime:\(bgmData?.currentTime ?? 0) 总时长\(bgmData?.duration ?? "")")
|
|
|
+ BFLog(message: "设置的音乐开始时间currentTime:\(bgmData?.currentTime ?? 0) 总时长\(bgmData?.duration ?? "") 音量是:\(bgmData?.volume ?? 0)")
|
|
|
resetWave()
|
|
|
- //默认进来播放音乐
|
|
|
+ // 默认进来播放音乐
|
|
|
playBGM()
|
|
|
-
|
|
|
+ avPlayer.volume = Float(bgmData?.volume ?? 0) / 100.0
|
|
|
addCutViewLayout()
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- lazy var titleL : UILabel = {
|
|
|
+
|
|
|
+ lazy var titleL: UILabel = {
|
|
|
let l = UILabel()
|
|
|
l.text = "起播点"
|
|
|
l.textAlignment = .center
|
|
|
l.textColor = .white
|
|
|
l.font = UIFont.systemFont(ofSize: 17, weight: .medium)
|
|
|
-
|
|
|
+
|
|
|
return l
|
|
|
}()
|
|
|
-
|
|
|
- // 删除 btn
|
|
|
- lazy var deleteBtn: UIButton = {
|
|
|
- let deleteBtn = UIButton()
|
|
|
- deleteBtn.backgroundColor = .clear
|
|
|
- deleteBtn.setTitleColor(.white, for: .normal)
|
|
|
- deleteBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
|
|
|
- deleteBtn.addTarget(self, action: #selector(deleteBtnAction), for: .touchDown)
|
|
|
-
|
|
|
- deleteBtn.setBackgroundImage(imageInRecordScreenKit(by: "voiceDeleteN"), for: .normal)
|
|
|
- deleteBtn.setBackgroundImage(imageInRecordScreenKit(by: "voiceDeleteS"), for: .selected)
|
|
|
- deleteBtn.adjustsImageWhenHighlighted = false
|
|
|
- return deleteBtn
|
|
|
- }()
|
|
|
-
|
|
|
+
|
|
|
required init?(coder _: NSCoder) {
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
}
|
|
@@ -141,8 +128,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
backView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 10)
|
|
|
addCutViewLayout()
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
func addCutViewLayout() {
|
|
|
let totalWidth = (frame.width - normalMargin * 2 - 6)
|
|
|
let itemWidth = totalWidth / CGFloat(Double("\(bgmData?.duration ?? "1")") ?? 0.0)
|
|
@@ -150,20 +136,17 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
cutRemindView.frame = CGRect(x: (panCutBackView.frame.width - 2) / 2, y: 0, width: 2, height: panCutBackView.frame.height)
|
|
|
currentProgressLab.centerX = panCutBackView.centerX
|
|
|
}
|
|
|
-
|
|
|
- func playBGM(){
|
|
|
|
|
|
- if(bgmData?.musicPath ?? "").count > 0{
|
|
|
+ func playBGM() {
|
|
|
+ if (bgmData?.musicPath ?? "").count > 0 {
|
|
|
avPlayer.pause()
|
|
|
avPlayer.replaceCurrentItem(with: AVPlayerItem(url: URL(string: (bgmData?.musicPath ?? ""))!))
|
|
|
- avPlayer.seek(to: CMTime.init(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale)))
|
|
|
-
|
|
|
+ avPlayer.seek(to: CMTime(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale)))
|
|
|
+
|
|
|
avPlayer.play()
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
override init(frame: CGRect) {
|
|
|
super.init(frame: frame)
|
|
|
|
|
@@ -194,7 +177,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
make.height.equalTo(24)
|
|
|
make.top.equalToSuperview().offset(18)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
backView.addSubview(titleL)
|
|
|
titleL.snp.makeConstraints { make in
|
|
|
make.centerX.equalToSuperview()
|
|
@@ -202,9 +185,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
make.height.equalTo(24)
|
|
|
make.top.equalTo(cancelBtn)
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
// 确认
|
|
|
let okBtn = UIButton()
|
|
|
okBtn.backgroundColor = .clear
|
|
@@ -219,15 +200,15 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
make.height.equalTo(24)
|
|
|
make.top.equalToSuperview().offset(18)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
backView.addSubview(panCutBackView)
|
|
|
panCutBackView.addSubview(cutRemindView)
|
|
|
backView.addSubview(currentProgressLab)
|
|
|
backView.addSubview(progressImage)
|
|
|
-
|
|
|
+
|
|
|
if (progressImage.layer.sublayers?.count ?? 0) > 0 {
|
|
|
let totalCount = progressImage.layer.sublayers?.count ?? 0
|
|
|
- for index in 0..<Int(totalCount) {
|
|
|
+ for index in 0 ..< Int(totalCount) {
|
|
|
(progressImage.layer.sublayers?[index] as? CAShapeLayer)?.strokeColor = UIColor.white.cgColor
|
|
|
(progressImage.layer.sublayers?[index] as? CAShapeLayer)?.setNeedsDisplay()
|
|
|
}
|
|
@@ -235,18 +216,16 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
let totalCount = Int(cScreenWidth - normalMargin * 2) / (cFrequency.count * 2)
|
|
|
let remainder = Int(cScreenWidth - normalMargin * 2) % (cFrequency.count * 2)
|
|
|
var totalWave: [CGFloat] = Array<CGFloat>.init()
|
|
|
- for _ in 0..<totalCount {
|
|
|
+ for _ in 0 ..< totalCount {
|
|
|
totalWave = totalWave + cFrequency
|
|
|
}
|
|
|
if remainder > 0 {
|
|
|
- totalWave = totalWave + cFrequency[0...(remainder / 2)]
|
|
|
+ totalWave = totalWave + cFrequency[0 ... (remainder / 2)]
|
|
|
}
|
|
|
createWave(waveArr: totalWave)
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// 生成波纹
|
|
|
/// - Parameter waveArr: <#waveArr description#>
|
|
|
/// - Returns: <#description#>
|
|
@@ -282,7 +261,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
progressImage.layer.addSublayer(lineLayer)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@objc func panClick(ges: UIPanGestureRecognizer) {
|
|
|
if ges.state == .changed {
|
|
|
let translation = ges.translation(in: backView)
|
|
@@ -307,13 +286,13 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
BFLog(message: "裁剪背景音乐 duration = \(bgmData?.duration ?? "0"),startTime = \(startTime),endTime = \(endTime)")
|
|
|
bgmData?.currentTime = startTime
|
|
|
|
|
|
-
|
|
|
avPlayer.seek(to: CMTime(value: CMTimeValue(startTime * playerTimescale), timescale: CMTimeScale(playerTimescale)))
|
|
|
avPlayer.play()
|
|
|
// 重置波纹
|
|
|
resetWave()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/// 重置波纹
|
|
|
/// - Returns: <#description#>
|
|
|
func resetWave() {
|
|
@@ -323,35 +302,31 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
}
|
|
|
let totalCount = progressImage.layer.sublayers?.count ?? 0
|
|
|
if totalCount > 0 {
|
|
|
- for index in 0..<Int(totalCount) {
|
|
|
-
|
|
|
+ for index in 0 ..< Int(totalCount) {
|
|
|
if index < Int(startTotal) {
|
|
|
(progressImage.layer.sublayers?[index] as? CAShapeLayer)?.strokeColor = UIColor.hexColor(hexadecimal: "#202020").cgColor
|
|
|
} else {
|
|
|
-
|
|
|
-
|
|
|
(progressImage.layer.sublayers?[index] as? CAShapeLayer)?.strokeColor = UIColor.white.cgColor
|
|
|
-
|
|
|
}
|
|
|
(progressImage.layer.sublayers?[totalCount - index - 1] as? CAShapeLayer)?.setNeedsDisplay()
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
func configPlayProgress(currentTime: Float64) {
|
|
|
let totalCount = progressImage.layer.sublayers?.count ?? 0
|
|
|
- if currentTime >= 0 {
|
|
|
- //播放的开始点
|
|
|
+ if currentTime >= 0 {
|
|
|
+ // 播放的开始点
|
|
|
let startPoint = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * Float64(bgmData?.currentTime ?? 0)
|
|
|
- //已经播放到的点
|
|
|
+ // 已经播放到的点
|
|
|
let endPoint = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * currentTime
|
|
|
-
|
|
|
- if(endPoint < startPoint){
|
|
|
+
|
|
|
+ if endPoint < startPoint {
|
|
|
BFLog(message: "startPoint: \(startPoint) endPoint:\(endPoint)")
|
|
|
return
|
|
|
}
|
|
|
if totalCount > 0 {
|
|
|
- for index in Int(startPoint)...Int(endPoint) {
|
|
|
+ for index in Int(startPoint) ... Int(endPoint) {
|
|
|
var tempIndex = index
|
|
|
if tempIndex < 0 {
|
|
|
tempIndex = 0
|
|
@@ -359,7 +334,6 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
if tempIndex >= totalCount - 1 {
|
|
|
tempIndex = totalCount - 1
|
|
|
}
|
|
|
- BFLog(message: "indesssssss \(tempIndex)")
|
|
|
(progressImage.layer.sublayers?[tempIndex] as? CAShapeLayer)?.strokeColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
|
|
|
(progressImage.layer.sublayers?[tempIndex] as? CAShapeLayer)?.setNeedsDisplay()
|
|
|
}
|
|
@@ -367,12 +341,8 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func show(){
|
|
|
+ func show() {
|
|
|
isHidden = false
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@objc func hidden() {
|
|
@@ -382,30 +352,17 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
|
|
|
|
|
|
@objc func cancelAction() {
|
|
|
hidden()
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
|
|
|
@objc func okBtnAction() {
|
|
|
hidden()
|
|
|
|
|
|
- bgmData?.startCMTime = CMTime.init(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale))
|
|
|
-
|
|
|
+ bgmData?.startCMTime = CMTime(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale))
|
|
|
+
|
|
|
BFLog(message: "最后设置的开始时间是\(bgmData?.startCMTime.seconds ?? 0.0)")
|
|
|
-
|
|
|
- if cutTimeHandle != nil,bgmData != nil {
|
|
|
+
|
|
|
+ if cutTimeHandle != nil, bgmData != nil {
|
|
|
cutTimeHandle!(0, 0, bgmData)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- @objc func deleteBtnAction() {
|
|
|
-
|
|
|
-// if cutTimeHandle != nil {
|
|
|
-// cutTimeHandle!(.MusiceCutActionDelete, selectVoice,false)
|
|
|
-// }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
-
|