|
@@ -9,6 +9,7 @@
|
|
|
import UIKit
|
|
|
import JXSegmentedView
|
|
|
import AliyunVideoSDKPro
|
|
|
+import CoreMotion
|
|
|
|
|
|
class PublishVideoRecorderController: BaseViewController {
|
|
|
|
|
@@ -18,12 +19,54 @@ class PublishVideoRecorderController: BaseViewController {
|
|
|
// 滤镜Items
|
|
|
var effectFilterItems: Array<AliyunEffectFilter> = []
|
|
|
|
|
|
+ // 视频片段管理器
|
|
|
+ var clipManager: AliyunClipManager?
|
|
|
+
|
|
|
+ // 开始录制时间
|
|
|
+ var downTime: Double?
|
|
|
+
|
|
|
+ // 结束录制时间
|
|
|
+ var upTime: Double?
|
|
|
+
|
|
|
+ // 开始录制视频段数
|
|
|
+ var downVideoCount: Int = 0
|
|
|
+
|
|
|
+ // 结束录制视频段数
|
|
|
+ var upVideoCount: Int = 0
|
|
|
+
|
|
|
+ // 录制时间
|
|
|
+ var recordingDuration: CFTimeInterval?
|
|
|
+
|
|
|
+ var motionManager: CMMotionManager?
|
|
|
+
|
|
|
+ // 相机旋转角度
|
|
|
+ var cameraRotate: Int = 90
|
|
|
+
|
|
|
+ // 录制时间区间
|
|
|
+ let maxDuration: CGFloat = 60.0
|
|
|
+ let minDuration: CGFloat = 3.0
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
setupViews()
|
|
|
setupLayouts()
|
|
|
setupFilterEffectData()
|
|
|
+
|
|
|
+ //录制片段设置
|
|
|
+ clipManager = recorder.clipManager
|
|
|
+ clipManager?.maxDuration = maxDuration
|
|
|
+ clipManager?.minDuration = minDuration
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewWillAppear(_ animated: Bool) {
|
|
|
+ startRetainCameraRotate()
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewWillDisappear(_ animated: Bool) {
|
|
|
+ motionManager?.stopDeviceMotionUpdates()
|
|
|
}
|
|
|
|
|
|
deinit {
|
|
@@ -50,6 +93,40 @@ class PublishVideoRecorderController: BaseViewController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func startRetainCameraRotate() {
|
|
|
+ if motionManager == nil {
|
|
|
+ motionManager = CMMotionManager()
|
|
|
+ }
|
|
|
+
|
|
|
+ if motionManager?.isDeviceMotionAvailable ?? false {
|
|
|
+ motionManager?.deviceMotionUpdateInterval = 1
|
|
|
+ motionManager?.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: {
|
|
|
+ [weak self] (motion, error) in
|
|
|
+ // Gravity 获取手机的重力值在各个方向上的分量,根据这个就可以获得手机的空间位置,倾斜角度等
|
|
|
+ let gravityX: Double = motion?.gravity.x ?? 0
|
|
|
+ let gravityY: Double = motion?.gravity.y ?? 0
|
|
|
+ //手机旋转角度
|
|
|
+ let xyTheta: Double = atan2(gravityX,gravityY)/Double.pi*180.0
|
|
|
+
|
|
|
+ if (xyTheta >= -45 && xyTheta <= 45) {
|
|
|
+ //down
|
|
|
+ self?.cameraRotate = 180;
|
|
|
+ } else if (xyTheta > 45 && xyTheta < 135) {
|
|
|
+ //left
|
|
|
+ self?.cameraRotate = 90;
|
|
|
+ } else if ((xyTheta >= 135 && xyTheta < 180) || (xyTheta >= -180 && xyTheta < -135)) {
|
|
|
+ //up
|
|
|
+ self?.cameraRotate = 0;
|
|
|
+ } else if (xyTheta >= -135 && xyTheta < -45) {
|
|
|
+ //right
|
|
|
+ self?.cameraRotate = 270;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // SDK录制类
|
|
|
private lazy var recorder: AliyunIRecorder = {
|
|
|
//清除之前生成的录制路径
|
|
|
let recordDir: String = AliyunPathManager.createRecrodDir()
|
|
@@ -60,7 +137,7 @@ class PublishVideoRecorderController: BaseViewController {
|
|
|
let videoSavePath: String = taskPath.appending(AliyunPathManager.randomString()).appending("mp4")
|
|
|
|
|
|
|
|
|
- let recorder = AliyunIRecorder.init(delegate: self as? AliyunIRecorderDelegate, videoSize: CGSize(width: 720, height: 1280))
|
|
|
+ let recorder = AliyunIRecorder.init(delegate: self, videoSize: CGSize(width: 720, height: 1280))
|
|
|
|
|
|
// 预览视图,必须设置
|
|
|
recorder?.preview = videoCameraView.previewView
|
|
@@ -174,29 +251,82 @@ extension PublishVideoRecorderController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 点击录制button开始录制
|
|
|
func recordButtonRecordVideo() {
|
|
|
+ print("---------->点击了录制按钮")
|
|
|
+
|
|
|
recorder.startRecording()
|
|
|
+
|
|
|
+ downTime = CFAbsoluteTimeGetCurrent()
|
|
|
+ downVideoCount = clipManager?.partCount ?? 0
|
|
|
+ if clipManager?.partCount ?? 0 <= 0 {
|
|
|
+ recorder.cameraRotate = Int32(cameraRotate)
|
|
|
+ }
|
|
|
+
|
|
|
+ if recorder.startRecording() == 0 {
|
|
|
+ // 隐藏UI
|
|
|
+
|
|
|
+ } else {
|
|
|
+ print("---------->startRecording错误error:");
|
|
|
+ // 还原UI
|
|
|
+
|
|
|
+// self.magicCameraView.progressView.videoCount--;
|
|
|
+// [self.magicCameraView resetRecordButtonUI];
|
|
|
+// self.magicCameraView.recording = NO;
|
|
|
+// _magicCameraView.realVideoCount = [_clipManager partCount];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 点击录制button停止录制
|
|
|
func recordButtonPauseVideo() {
|
|
|
+ print("---------->点击了暂停录制按钮")
|
|
|
+
|
|
|
recorder.stopRecording()
|
|
|
+ upTime = CFAbsoluteTimeGetCurrent();
|
|
|
+
|
|
|
+ // 还原UI
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- // 收到录制停止回调,调用完成录制
|
|
|
func recordButtonFinishVideo() {
|
|
|
recorder.finishRecording()
|
|
|
}
|
|
|
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - AliyunIRecorderDelegate
|
|
|
+extension PublishVideoRecorderController: AliyunIRecorderDelegate {
|
|
|
+ func recorderDeviceAuthorization(_ status: AliyunIRecorderDeviceAuthor) {
|
|
|
+ if (status.rawValue == 1) {
|
|
|
+ SwiftProgressHUD.shared().showText("麦克风无权限")
|
|
|
+ } else if (status.rawValue == 2) {
|
|
|
+ SwiftProgressHUD.shared().showText("摄像头无权限")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 暂停录制回调
|
|
|
+ func recorderDidStopRecording() {
|
|
|
+ print("---------->暂停录制回调")
|
|
|
+
|
|
|
+ upVideoCount = clipManager?.partCount ?? 0
|
|
|
+
|
|
|
+ let vDura: CGFloat = CGFloat(recordingDuration ?? 0)
|
|
|
+ if vDura >= minDuration {
|
|
|
+ // 允许结束button点按
|
|
|
+ } else {
|
|
|
+ // 禁止结束button点按
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收到完成录制回调,视频已经保存到初始化设置的`视频输出沙盒路径`路径下
|
|
|
+ func recorderDidFinishRecording() {
|
|
|
+ print("----录制完成")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收到录制达到最大时长回调,调用完成录制
|
|
|
func recorderDidStopWithMaxDuration() {
|
|
|
- // 收到录制达到最大时长回调,调用完成录制
|
|
|
recorder.finishRecording()
|
|
|
print("----超时")
|
|
|
}
|
|
|
- func recorderDidFinishRecording() {
|
|
|
- // 收到完成录制回调,视频已经保存到初始化设置的`视频输出沙盒路径`路径下
|
|
|
- }
|
|
|
|
|
|
}
|
|
|
|