123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828 |
- //
- // PublishNewVideoPhotoView.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/9/3.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- // MARK: - 初始化/action
- class PublishNewVideoPhotoView: BaseView {
-
- /// 销毁View
- deinit {
- NXLLog("deinit")
- if observe != nil {
- NotificationCenter.default.removeObserver(observe!)
- }
- }
- weak var observe : NSObjectProtocol?
-
- /// 录制的时长
- var recorderDuration: CGFloat = 0.0
- /// 录制最大时长
- let maxDuration : CGFloat = 60.0
- /// 录制最小时长
- let minDuration : CGFloat = 3.0
- /// 滤镜选中的selected
- var selectedIndex : Int = 0
- /// 是否完成录制
- var isFinishRecorder : Bool = false
-
- /// 添加View
- override func setupViews() {
- backgroundColor = UIColor.black
- addSubview(recorderPreview)
- addSubview(publishNewVideoPhotoSetView)
- addSubview(effectFilterBeautyView)
- addSubview(musicView)
- }
-
- /// 布局
- override func setupLayouts() {
- publishNewVideoPhotoSetView.snp.makeConstraints { (make) in
- make.edges.equalToSuperview()
- }
- }
-
- /// 设置数据
- override func setupData() {
-
- // 返回
- publishNewVideoPhotoSetView.closeButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- self?.recorder?.stopPreview()
- let vc = UIViewController.topMost
- vc?.navigationController?.dismiss(animated: true, completion: nil)
- }).disposed(by: disposeBag)
- // 翻转
- publishNewVideoPhotoSetView.cameraFlipButton.rx.tap.subscribe(onNext: {[weak self] (data) in
- self?.setCameraFlip()
- }).disposed(by: disposeBag)
- // 滤镜
- publishNewVideoPhotoSetView.filterButton.rx.tap.subscribe(onNext: {[weak self] (data) in
- self?.effectFilterBeautyView.filterBeautyType = .filter
- self?.effectFilterBeautyViewisHidden(isHidden: false)
- }).disposed(by: disposeBag)
- // 美颜
- publishNewVideoPhotoSetView.beautyButton.rx.tap.subscribe(onNext: {[weak self] (data) in
- self?.effectFilterBeautyView.filterBeautyType = .beauty
- self?.effectFilterBeautyViewisHidden(isHidden: false)
- }).disposed(by: disposeBag)
- // 音乐
- publishNewVideoPhotoSetView.musicButton.rx.tap.subscribe(onNext: {[weak self] (data) in
- self?.musicViewisHidden(isHidden: false)
- }).disposed(by: disposeBag)
- // 闪光灯
- publishNewVideoPhotoSetView.flashButton.rx.tap.subscribe(onNext: {[weak self] (data) in
- if self?.recorder?.torchMode == .auto {
- self?.recorder?.switchTorch(with: AliyunIRecorderTorchMode.on)
- self?.publishNewVideoPhotoSetView.flashButton.setImage(kImage(name: "video_ico_flashlight"), for: UIControl.State.normal)
- } else if self?.recorder?.torchMode == .on {
- self?.recorder?.switchTorch(with: AliyunIRecorderTorchMode.off)
- self?.publishNewVideoPhotoSetView.flashButton.setImage(kImage(name: "video_ico_flashlight_close"), for: UIControl.State.normal)
- } else if self?.recorder?.torchMode == .off {
- self?.recorder?.switchTorch(with: AliyunIRecorderTorchMode.auto)
- self?.publishNewVideoPhotoSetView.flashButton.setImage(kImage(name: "video_ico_flashlight_auto"), for: UIControl.State.normal)
- }
- }).disposed(by: disposeBag)
- //录制/拍摄
- publishNewVideoPhotoSetView.recordButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- // 开始录制/暂停录制
- if PublishNewViewModel.shared.publishNewRecorderType == .video {
- if (self?.publishNewVideoPhotoSetView.recordButton.isSelected)! { //暂停拍摄
- self?.stopTakeVideo()
- }else { //开始拍摄
- self?.startTakeVideo()
- }
- }
- // 拍照
- if PublishNewViewModel.shared.publishNewRecorderType == .photo {
- // 拍照
- self?.takePhoto()
- //切换按钮状态
- self?.publishNewVideoPhotoSetView.recordButton.isSelected = false
- }
- }).disposed(by: disposeBag)
- // 回删视频
- publishNewVideoPhotoSetView.deleteButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- /// 回删视频
- self?.deleteVideo()
- }).disposed(by: disposeBag)
- // 完成录制
- publishNewVideoPhotoSetView.finishButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- //完成录制的view
- self?.finishRecorderView()
- if self?.recorder?.isRecording ?? true {
- // 完成录制需要先掉用停止录制在走delegate
- self?.stopTakeVideo()
- }else {
- self?.finishTakeVideo()
- }
-
- }).disposed(by: disposeBag)
- // 左滑 滤镜 加 1
- publishNewVideoPhotoSetView.leftSwipeClosure = {
- [weak self] in
- self?.filterPlus()
- }
- // 右滑 滤镜 减 1
- publishNewVideoPhotoSetView.rightSwipeClosure = {
- [weak self] in
- self?.filterLess()
- }
-
-
- // 美颜等级设置
- effectFilterBeautyView.effectBeautyView.didSelectCloSure = {
- [weak self] beautifyValue in
- self?.recorder?.beautifyValue = beautifyValue
- }
- // 美颜滤镜设置
- effectFilterBeautyView.effectFilterView.didSelectCloSure = {
- [weak self] (index, aliyunEffectFilter) in
- self?.selectedIndex = index
- if index == 0 {
- self?.recorder?.deleteFilter()
- }else {
- self?.recorder?.apply(aliyunEffectFilter)
- }
- self?.showFilterNameLabel(aliyunEffectFilter: aliyunEffectFilter)
- }
-
- // 关闭音乐按钮
- musicView.closeButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- PublishNewMusicView.publishNewMusicSelectedIndexPath = nil
- self?.musicViewisHidden(isHidden: true)
- }).disposed(by: disposeBag)
-
- // 不使用音乐按钮
- musicView.noUseMusicButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- PublishNewMusicView.publishNewMusicSelectedIndexPath = nil
- PublishNewMusicView.publishNewMusicUseIndexPath = nil
- let effectMusic : AliyunEffectMusic? = nil
- self?.recorder?.apply(effectMusic)
- self?.musicViewisHidden(isHidden: true)
- }).disposed(by: disposeBag)
-
- // 使用音乐
- musicView.userMusicClosure = {
- [weak self] in
- if PublishNewMusicView.publishNewMusicUseURL?.path == nil {
- PublishNewMusicView.publishNewMusicUseIndexPath = nil
- self?.musicViewisHidden(isHidden: true)
- SwiftProgressHUD.shared().showText("使用音乐错误,请您重新选择!!!")
- }else {
- PublishNewMusicView.publishNewMusicSelectedIndexPath = nil
- self?.recorder?.apply(AliyunEffectMusic(file: PublishNewMusicView.publishNewMusicUseURL?.path))
- self?.musicViewisHidden(isHidden: true)
- }
-
- }
-
-
- // 左滑 滤镜 加 1
- recorderPreview.leftSwipeClosure = {
- [weak self] in
- self?.filterPlus()
- }
- // 右滑 滤镜 减 1
- recorderPreview.rightSwipeClosure = {
- [weak self] in
- self?.filterLess()
- }
- // 点击手势
- recorderPreview.tapClosure = {
- [weak self] in
- if self?.effectFilterBeautyView.alpha == 1 {
- // 隐藏滤镜
- self?.effectFilterBeautyViewisHidden(isHidden: true)
- }
-
- if self?.musicView.alpha == 1 {
- // 隐藏音乐
- self?.musicViewisHidden(isHidden: true)
- }
- }
-
- // 对于锁屏,来电,退后台等事件,需要进行特殊处理。
- //当进入Inactive状态前需要调
- observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main) {[weak self] (_) in
- if self?.recorder?.isRecording ?? true {
- self?.stopTakeVideo()
- }
- self?.recorder?.stopPreview()
- }
- // 当进入Active状态
- observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main) {[weak self] (_) in
- self?.recorder?.stopPreview()
- }
-
- }
-
- /// SDK录制类
- lazy var recorder: AliyunIRecorder? = {
-
- let recorder = AliyunIRecorder.init(delegate: self, videoSize: CGSize(width: 720, height: 1280))
- // 预览View
- recorder?.preview = recorderPreview
- //创建文件夹路径
- let taskPathUrl =
- PathURLManager.projectDateFormatPath(dateFormat: "yyyyMM", parentURL:
- PathURLManager.projectVideosPath(parentURL:
- PathURLManager.projectResourcesPath(parentURL:
- PathURLManager.projectRecordssPath(parentURL:
- PathURLManager.projectDocumentPath()))))!
- //清除录制路径
- PathURLManager.clearPath(pathURL: taskPathUrl)
- // 设置文件夹路径
- recorder?.taskPath = taskPathUrl.path
-
- // 设置录制视频输出路径
- let outputPathUrl =
- PathURLManager.projectPathExtensionMp4(parentURL:
- PathURLManager.projectFilePath(fileName:
- PathURLManager.randomString(), parentURL:
- PathURLManager.projectDateFormatPath(dateFormat: "yyyyMM", parentURL:
- PathURLManager.projectVideosPath(parentURL:
- PathURLManager.projectResourcesPath(parentURL:
- PathURLManager.projectRecordssPath(parentURL:
- PathURLManager.projectDocumentPath()))))))!
- // 设置录制视频输出路径
- recorder?.outputPath = outputPathUrl.path
- //循环切换手电筒模式手电筒模式有三种,分别是off,on和auto。切换顺序:off —-> on —-> auto。默认值:off。
- recorder?.switchTorch(with: AliyunIRecorderTorchMode.off)
- // 美颜状态
- recorder?.beautifyStatus = true;
- // 设置美颜度 [0,100]
- recorder?.beautifyValue = 50
- // 前置摄像头采集分辨率 默认:AVCaptureSessionPreset1280x720
- recorder?.frontCaptureSessionPreset = AVCaptureSession.Preset.hd1280x720.rawValue
- // 后置摄像头采集分辨率 默认:AVCaptureSessionPreset1280x720
- recorder?.backCaptureSessionPreset = AVCaptureSession.Preset.hd1280x720.rawValue
- // 设置摄像头角度
- recorder?.cameraRotate = 0
- // 设置编码方式 0软编 1硬编
- recorder?.encodeMode = 0
- // 设置关键帧间隔
- recorder?.gop = 5
- // 设置关键帧间隔
- recorder?.recordFps = 25
- // 设置是否静音
- recorder?.mute = false
- // 设置视频质量 如果设置了bitrate参数,那么当前设置无效。
- recorder?.videoQuality = .hight
- // 设置输出视频码率(bps)
- recorder?.bitrate = 2000000
- // 设置采集视频格式
- recorder?.outputType = .type420f
- // 设置视频最大时长 默认8
- recorder?.clipManager.maxDuration = maxDuration
- // 设置视频最小时长 默认8
- recorder?.clipManager.minDuration = minDuration
- return recorder!
- }()
-
- /// 摄像头
- lazy var recorderPreview : PublishNewRecorderPreview = {
- let recorderPreview = PublishNewRecorderPreview(frame: kScreenBounds)
- return recorderPreview
- }()
- /// 所有按钮View
- lazy var publishNewVideoPhotoSetView: PublishNewVideoPhotoSetView = {
- let publishNewVideoPhotoSetView = PublishNewVideoPhotoSetView()
- return publishNewVideoPhotoSetView
- }()
-
- /// 滤镜跟美颜
- lazy var effectFilterBeautyView: PublishNewEffectFilterBeautyView = {
- let effectFilterBeautyView = PublishNewEffectFilterBeautyView(frame: CGRect(x: 0, y: kScreenHeight, width: kScreenWidth, height: 48 + 125 + kSafeTabBarHeight))
- return effectFilterBeautyView
- }()
-
- /// 音乐
- lazy var musicView : PublishNewMusicView = {
- let musicView = PublishNewMusicView(frame: CGRect(x: 0, y: kScreenHeight, width: kScreenWidth, height: kScreenHeight - kSafeStatusBarHeight))
- return musicView
- }()
-
-
- }
- // MARK: - 方法
- extension PublishNewVideoPhotoView {
-
- /// 开始拍视频
- func startTakeVideo() {
-
- self.recorder?.startRecording()
- DispatchQueue.main.async {
- [weak self] in
- // 回删按钮隐藏
- self?.deleteAlpha(alpha: 0)
- // 分段view隐藏
- self?.segmentedViewAlpha(alpha: 0)
- // 开始录制隐藏View
- self?.startTakeVideoHiddenView()
- }
-
- }
-
- /// 暂停拍视频
- func stopTakeVideo() {
- self.recorder?.stopRecording()
- }
-
- /// 开始录制隐藏View
- func startTakeVideoHiddenView() {
- publishNewVideoPhotoSetView.progressView.videoCount += 1
- publishNewVideoPhotoSetView.closeButton.isHidden = true
- publishNewVideoPhotoSetView.cameraFlipButton.isHidden = true
- publishNewVideoPhotoSetView.filterButton.isHidden = true
- publishNewVideoPhotoSetView.beautyButton.isHidden = true
- publishNewVideoPhotoSetView.musicButton.isHidden = true
- //切换按钮状态
- publishNewVideoPhotoSetView.recordButton.isSelected = !publishNewVideoPhotoSetView.recordButton.isSelected
- }
-
- /// 停止录制显示View
- func stopTakeVideoVideoHiddenView() {
- publishNewVideoPhotoSetView.closeButton.isHidden = false
- publishNewVideoPhotoSetView.cameraFlipButton.isHidden = false
- publishNewVideoPhotoSetView.filterButton.isHidden = false
- publishNewVideoPhotoSetView.beautyButton.isHidden = false
- publishNewVideoPhotoSetView.musicButton.isHidden = false
- //切换按钮状态
- publishNewVideoPhotoSetView.recordButton.isSelected = !publishNewVideoPhotoSetView.recordButton.isSelected
- }
-
- /// 完成拍视频
- func finishTakeVideo() {
- self.recorder?.finishRecording()
- }
-
- /// 拍照
- func takePhoto() {
- recorder?.takePhoto({(image, rawImage) in
- DispatchQueue.main.async {
- // 跳转到使用照片页面
- let publishConfirmPhotoNewVC = PublishConfirmPhotoNewViewController()
- publishConfirmPhotoNewVC.photoImg = image
- UIViewController.topMost?.navigationController?.pushViewController(publishConfirmPhotoNewVC, animated: true)
- }
- })
- }
- /// 回删视频
- func deleteVideo() {
- DispatchQueue.main.async {
- [weak self] in
- if self?.recorder?.clipManager?.partCount ?? 0 > 0 {
- AlertSheetView.alert(title: "是否删除上一片段?") { [weak self] (popupView, index, title) in
- let partCount = self?.recorder?.clipManager?.partCount
- // 删除最后一个视频片段
- self?.recorder?.clipManager?.deletePart()
- // 视频时长
- self?.recorderDuration = (self?.recorder?.clipManager?.duration)!
- // 完成按钮是否显示
- self?.finishAlpha()
- // 时间按钮是否显示
- self?.durationTimeAlpha()
- // 设置回删的进度View
- self?.publishNewVideoPhotoSetView.progressView.videoCount -= 1
- // 设置回删的进度View
- self?.setProgress()
- // 剩下最后一个片段视频
- if partCount == 1 {
- // 回删按钮是否隐藏
- self?.deleteAlpha(alpha: 0)
- // 分段view隐藏
- self?.segmentedViewAlpha(alpha: 1)
- }
- }
- }
- }
- }
-
- /// 回删按钮显示跟隐藏
- /// - Parameter alpha: 回删按钮显示跟隐藏
- func deleteAlpha(alpha:CGFloat) {
- DispatchQueue.main.async {
- [weak self] in
- self?.publishNewVideoPhotoSetView.deleteButton.alpha = alpha
- }
- }
-
- /// 完成按钮显示跟隐藏
- /// - Parameter alpha: 完成按钮显示跟隐藏
- func finishAlpha() {
- DispatchQueue.main.async {
- [weak self] in
- if (self?.recorderDuration)! >= 3.0 {
- if (self?.publishNewVideoPhotoSetView.finishButton.isHidden)! {
- self?.publishNewVideoPhotoSetView.finishButton.isHidden = false
- }
- }else {
- if !(self?.publishNewVideoPhotoSetView.finishButton.isHidden)! {
- self?.publishNewVideoPhotoSetView.finishButton.isHidden = true
- }
- }
- }
- }
-
- /// 时间label显示跟隐藏
- /// - Parameter alpha: 时间label显示跟隐藏
- func durationTimeAlpha() {
- DispatchQueue.main.async {
- [weak self] in
- if (self?.recorderDuration)! > 0.0 {
- if (self?.publishNewVideoPhotoSetView.durationTimeLabel.isHidden)! {
- self?.publishNewVideoPhotoSetView.durationTimeLabel.isHidden = false
- self?.publishNewVideoPhotoSetView.durationTimeDotLabel.isHidden = false
- }
- let d : Int = Int(self?.recorderDuration ?? 0)
- let m : Int = Int(d / 60)
- let s : Int = Int(d % 60)
- self?.publishNewVideoPhotoSetView.durationTimeLabel.text = String(format: "%02d:%02d", m,s)
- }else {
- if !(self?.publishNewVideoPhotoSetView.durationTimeLabel.isHidden)! {
- self?.publishNewVideoPhotoSetView.durationTimeLabel.isHidden = true
- self?.publishNewVideoPhotoSetView.durationTimeDotLabel.isHidden = true
- }
- }
- }
- }
-
- /// 设置时间进度
- func setProgress() {
- DispatchQueue.main.async {
- [weak self] in
- self?.publishNewVideoPhotoSetView.progressView.progress = self?.recorderDuration ?? 0
- }
- }
-
- /// 设置进度条是否显示
- ///
- /// - Parameter alpha: 透明度
- func setProgressViewAlpha(alpha:CGFloat,duration:TimeInterval) {
- UIView.animate(withDuration: duration) {
- [weak self] in
- self?.publishNewVideoPhotoSetView.progressView.alpha = alpha
- }
- // 更新关闭/闪光灯/音乐按钮的位置
- closeMakeConstraints(duration: duration)
- }
-
- /// 更新关闭/闪光灯/音乐按钮的位置
- ///
- /// - Parameter duration: 0.5s
- func closeMakeConstraints(duration:TimeInterval) {
- if PublishNewViewModel.shared.publishNewRecorderType == .video {
- publishNewVideoPhotoSetView.closeVideoUpMakeConstraints(duration: duration)
- // 设置闪光灯icon
- recorder?.switchTorch(with: AliyunIRecorderTorchMode.off)
- publishNewVideoPhotoSetView.flashButton.setImage(kImage(name: "video_ico_flashlight_close"), for: UIControl.State.normal)
- }else if PublishNewViewModel.shared.publishNewRecorderType == .photo {
- publishNewVideoPhotoSetView.closePhotoUpMakeConstraints(duration: duration)
- // 设置闪光灯icon
- recorder?.switchTorch(with: AliyunIRecorderTorchMode.auto)
- publishNewVideoPhotoSetView.flashButton.setImage(kImage(name: "video_ico_flashlight_auto"), for: UIControl.State.normal)
- }
- }
-
- /// 设置相机翻转
- func setCameraFlip() {
- let cameraPosition = recorder?.cameraPosition
- if cameraPosition == .front {
- UIView.transition(with: recorderPreview, duration: 0.5, options: UIView.AnimationOptions.transitionFlipFromLeft, animations: {
- [weak self] in
- self?.recorder?.switchCameraPosition()
- }, completion: { _ in})
- }else {
- UIView.transition(with: recorderPreview, duration: 0.5, options: UIView.AnimationOptions.transitionFlipFromRight, animations: {
- [weak self] in
- self?.recorder?.switchCameraPosition()
- }, completion: { _ in})
- }
- }
-
- /// 分段view显示
- /// - Parameter alpha: 透明度
- func segmentedViewAlpha(alpha:CGFloat) {
- UIView.animate(withDuration: 0.5) {
- PublishNewViewModel.shared.segmentedView?.alpha = alpha
- }
- }
-
-
- /// 滤镜美颜是否显示
- ///
- /// - Parameter isHidden: 是否隐藏
- func effectFilterBeautyViewisHidden(isHidden:Bool) {
- var offsetY : CGFloat = kScreenHeight
- if isHidden {
- offsetY = kScreenHeight
- if recorderDuration == 0 {
- segmentedViewAlpha(alpha:1)
- }else {
- segmentedViewAlpha(alpha:0)
- }
- // 移除点击手势
- recorderPreview.removetap()
- }else {
- offsetY = kScreenHeight-(48 + 125 + kSafeTabBarHeight)
- segmentedViewAlpha(alpha:0)
- // 添加点击手势
- recorderPreview.addTap()
- }
- UIView.animate(withDuration: 0.5) {
- [weak self] in
- self?.effectFilterBeautyView.frame = CGRect(x: 0, y: offsetY, width: kScreenWidth, height: 48 + 125 +
- kSafeTabBarHeight)
- if isHidden {
- self?.effectFilterBeautyView.alpha = 0
- self?.publishNewVideoPhotoSetView.alpha = 1
- }else {
- self?.effectFilterBeautyView.alpha = 1
- self?.publishNewVideoPhotoSetView.alpha = 0
- }
- }
- }
-
- //滤镜 减 1
- func filterLess() {
- if selectedIndex > 0 {
- selectedIndex = selectedIndex - 1
- }else {
- selectedIndex = (aliyunFiltersModel?.filters?.count ?? 1) - 1
- }
- //选中滤镜
- didSelectFilter()
- }
-
- //滤镜 加 1
- func filterPlus() {
- if selectedIndex < ((aliyunFiltersModel?.filters?.count ?? 0) - 1) {
- selectedIndex = selectedIndex + 1
- }else {
- selectedIndex = 0
- }
- //选中滤镜
- didSelectFilter()
- }
-
- /// 选中滤镜
- func didSelectFilter() {
- let indexPath = IndexPath(row: self.selectedIndex , section: 0)
- effectFilterBeautyView.effectFilterView.collectionView(effectFilterBeautyView.effectFilterView.collectionView, didSelectItemAt: indexPath)
- effectFilterBeautyView.effectFilterView.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)
- }
- /// 滤镜名字显示
- ///
- /// - Parameter aliyunEffectFilter: 滤镜模型
- func showFilterNameLabel(aliyunEffectFilter:AliyunEffectFilter) {
-
- recorderPreview.filterNameLabel.alpha = 1
- recorderPreview.filterNameLabel.text = aliyunEffectFilter.name
- UIView.animate(withDuration: 2, animations: {
- [weak self] in
- self?.recorderPreview.filterNameLabel.alpha = 1
- }, completion: {
- [weak self] (finished) in
- UIView.animate(withDuration: 2, animations: {
- [weak self] in
- self?.recorderPreview.filterNameLabel.alpha = 0
- })
- })
- }
-
- /// 音乐是否显示
- ///
- /// - Parameter isHidden: 是否隐藏
- func musicViewisHidden(isHidden:Bool) {
- AliPlayerManager.shared.audioPlayer.stop()
- recorder?.startPreview()
- var offsetY : CGFloat = kScreenHeight
- if isHidden {
- offsetY = kScreenHeight
- if recorderDuration == 0 {
- segmentedViewAlpha(alpha:1)
- }else {
- segmentedViewAlpha(alpha:0)
- }
- // 移除点击手势
- recorderPreview.removetap()
- musicView.listContainerView.reloadData()
- }else {
- offsetY = kSafeStatusBarHeight
- segmentedViewAlpha(alpha:0)
- // 添加点击手势
- recorderPreview.addTap()
- }
- UIView.animate(withDuration: 0.5, animations: {
- [weak self] in
- self?.musicView.frame = CGRect(x: 0, y: offsetY, width: kScreenWidth, height: kScreenHeight -
- kSafeStatusBarHeight)
- if isHidden {
- self?.musicView.alpha = 0
- self?.publishNewVideoPhotoSetView.alpha = 1
- }else {
- self?.musicView.alpha = 1
- self?.publishNewVideoPhotoSetView.alpha = 0
- }
- })
- }
-
- /// 结束录制回调/停止录制回调
- func stopRecorderShowHidden() {
- // 回删按钮透明度
- deleteAlpha(alpha:1)
- // 停止录制显示View
- DispatchQueue.main.async {
- [weak self] in
- self?.stopTakeVideoVideoHiddenView()
- }
- }
-
- /// 完成录制的View
- func finishRecorderView() {
- SwiftProgressHUD.shared().showWait("视频正在合成中...")
- isFinishRecorder = true
- }
- }
- // MARK: - 代理
- extension PublishNewVideoPhotoView : AliyunIRecorderDelegate {
-
- /// 授权代理
- /// - Parameter status: 收取状态
- func recorderDeviceAuthorization(_ status: AliyunIRecorderDeviceAuthor) {
- switch status {
- // 授权同意
- case .enabled:
- break
- // 音频授权被拒绝
- case .audioDenied:
- break
- // 视频授权被拒绝
- case .videoDenied:
- break
- default:
- break
- }
- }
-
- /**
- 摄像头返回的原始视频数据
-
- 开放出来的目的是用于做人脸识别
- @param sampleBuffer 视频数据
- */
- // func recorderOutputVideoRawSampleBuffer(_ sampleBuffer: CMSampleBuffer!)
-
- /**
- 返回原始的音频数据
-
- 用来做语音识别一类的业务
- @param sampleBuffer 音频数据
- */
- // func recorderOutputAudioRawSampleBuffer(_ sampleBuffer: CMSampleBuffer!)
-
- /**
- 用户自定义渲染
-
- @param sampleBuffer 原始视频数据
- @return 用户自渲染后的PixelBuffer
- */
- // optional func customRenderedPixelBuffer(withRawSampleBuffer sampleBuffer: CMSampleBuffer!) -> Unmanaged<CVPixelBuffer>!
-
- /**
- 用户自定义渲染
-
- 开放pixelBuffer和纹理id给用户自渲染 (仅支持BGRA格式)
- @param pixelBuffer 摄像头数据
- @param textureName 摄像头数据纹理
- @return 自定义渲染后的纹理id
- */
- // optional func recorderOutputVideoPixelBuffer(_ pixelBuffer: CVPixelBuffer!, textureName: Int) -> Int
-
- /**
- 用户自定义渲染接口
-
- @param srcTexture 原始视频帧纹理id
- @param size 原始视频帧纹理size
- @return 返回纹理id
- */
- // optional func customRender(_ srcTexture: Int32, size: CGSize) -> Int32
-
- /**
- 录制实时时长
-
- @param duration 录制时长
- */
- func recorderVideoDuration(_ duration: CGFloat) {
- // 录制时长
- recorderDuration = duration
- // 完成按钮是否显示
- finishAlpha()
- // 时间按钮是否显示
- durationTimeAlpha()
- // 时间进度
- setProgress()
- }
-
- /**
- 摄像头返回的原始视频纹理
-
- 摄像头数据格式为BGRA、YUV时都需实现
- @param textureName 原始纹理ID
- @return 处理后的纹理ID
- */
- // optional func recorderOutputVideoTextureName(_ textureName: Int, textureSize textureSie: CGSize) -> Int
-
- /**
- 摄像头返回的原始视频纹理
-
- 摄像头数据格式仅为YUV时须实现,反之不实现
- @param textureName 原始UV分量的纹理ID
- @return 处理后的纹理ID
- */
- // optional func recorderOutputVideoUVTextureName(_ textureName: Int) -> Int
-
- /**
- 开始预览回调
- */
- func recorderDidStartPreview() {
- }
-
- /**
- 停止录制回调
- */
- func recorderDidStopRecording() {
- //是否完成录制
- if isFinishRecorder { //完成 显示view也要完成视频合成
- finishTakeVideo()
- }else{ //没有完成只改变view显示
- stopRecorderShowHidden()
- }
- }
-
- /**
- 当录至最大时长时回调
- */
- func recorderDidStopWithMaxDuration() {
- finishRecorderView()
- finishTakeVideo()
- }
-
- /**
- 结束录制回调
- */
- func recorderDidFinishRecording() {
- SwiftProgressHUD.shared().hide()
- if isFinishRecorder { //完成 显示view也要完成视频合成
- stopRecorderShowHidden()
- }
- isFinishRecorder = false
- DispatchQueue.main.async {
- [weak self] in
- self?.publishNewVideoPhotoSetView.recordButton.isSelected = false
- let vc = PublishEditNewViewController()
- vc.taskPath = self?.recorder?.taskPath
- vc.popClosure = {
- [weak self] in
- self?.recorder?.startPreview()
- }
- UIViewController.topMost?.navigationController?.pushViewController(vc, animated: true)
- }
-
- }
-
- /**
- 录制异常
-
- @param error 异常
- */
- func recoderError(_ error: Error!) {
- NXLLog(error)
- SwiftProgressHUD.shared().showText("录制异常")
- }
- }
|