INIntroduceController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // INIntrocudeController.swift
  3. // Introduce
  4. //
  5. // Created by 胡志强 on 2021/11/29.
  6. // 制作录音,预览页面
  7. import BFCommonKit
  8. import BFRecordScreenKit
  9. import Foundation
  10. import Photos
  11. import UIKit
  12. import BFAnalyzeKit
  13. import BFUIKit
  14. class INIntroduceController: BFBaseViewController {
  15. lazy var stripSwithView : BFStripSwithView = {
  16. let v = BFStripSwithView(frame: .zero, items: 0, space: 4)
  17. v.itemClickHandle = { [weak self] _, index in
  18. guard let wself = self else { return }
  19. wself.recordScreenVC.updateContentOffset(index: index)
  20. }
  21. return v
  22. }()
  23. let exportBtn = UIButton()
  24. var assets: [PHAsset]?
  25. let recordScreenVC = BFRecordScreenController()
  26. lazy var addMaterialBtn : UIButton = {
  27. let btn = UIButton()
  28. btn.setImage(UIImage(named: "dl_add"), for: .normal)
  29. btn.addTarget(self, action: #selector(addmaterial), for: .touchUpInside)
  30. return btn
  31. }()
  32. // MARK: - VC生命周期
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35. showNavigation()
  36. PQNotification.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  37. PQNotification.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  38. }
  39. override func viewWillDisappear(_ animated: Bool) {
  40. super.viewWillDisappear(animated)
  41. PQNotification.removeObserver(self)
  42. cHiddenHUB(superView: nil)
  43. }
  44. @objc internal func keyboardWillHide(_: Notification?) {
  45. navHeadImageView?.isHidden = false
  46. }
  47. @objc internal func keyboardWillShow(_: Notification?) {
  48. navHeadImageView?.isHidden = true
  49. }
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. navHeadImageView?.backgroundColor = .clear
  53. navHeadImageView?.image = UIImage(named: "top_shadow")
  54. navHeadImageView?.contentMode = .scaleToFill
  55. leftButton(image: nil, imageName: nil, tintColor: .white)
  56. exportBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#389AFF")
  57. exportBtn.setTitle("option_export".BFLocale, for: .normal)
  58. exportBtn.addCorner(corner: 4)
  59. exportBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
  60. exportBtn.addTarget(self, action: #selector(exportAction), for: .touchUpInside)
  61. navHeadImageView?.addSubview(exportBtn)
  62. exportBtn.frame = CGRect(x: (navHeadImageView?.frame.width ?? 0) - 12 - 60, y: 0, width: 60, height: 36)
  63. exportBtn.center.y = backButton?.center.y ?? 0
  64. exportBtn.titleLabel?.adjustsFontSizeToFitWidth = true
  65. addChild(recordScreenVC)
  66. recordScreenVC.view.frame = view.frame
  67. view.addSubview(recordScreenVC.view)
  68. if let ass = assets {
  69. recordScreenVC.fetchMaterial(ass)
  70. }
  71. recordScreenVC.changeItemHandle = { [weak self] index in
  72. guard let wself = self else { return }
  73. // 删除了一条
  74. if index == -3 {
  75. wself.resetStripSwithView(wself.recordScreenVC.itemModels.count)
  76. }else{
  77. wself.stripSwithView.changeSwitchStatus(index: index)
  78. }
  79. }
  80. recordScreenVC.recordEndHandle = {model in
  81. // 录音完成时上报
  82. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_speekButton, pageSource: .sp_speekPage,extParams: ["duration":Int(((model?.endCMTime.seconds ?? 0) - (model?.startCMTime.seconds ?? 0)) * 1000)],commonParams: commonParams())
  83. }
  84. recordScreenVC.subTitleBtnClickHandle = { isOn in
  85. // 点击字幕按钮上报
  86. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_captionsButton, pageSource: .sp_speekPage,extParams: ["isSubtitlesClose":isOn],commonParams: commonParams())
  87. }
  88. navHeadImageView?.addSubview(stripSwithView)
  89. navHeadImageView?.addSubview(addMaterialBtn)
  90. stripSwithView.snp.makeConstraints { make in
  91. make.left.equalTo(backButton!.snp.right).offset(5)
  92. make.top.height.equalTo(backButton!)
  93. if (assets?.count ?? 0) > 1 {
  94. make.width.equalTo(cScreenWidth).priority(.high)
  95. }else{
  96. make.width.equalTo(0).priority(.high)
  97. }
  98. }
  99. addMaterialBtn.snp.makeConstraints { make in
  100. make.left.equalTo(stripSwithView.snp.right).offset(-2)
  101. make.top.height.equalTo(backButton!)
  102. if (assets?.count ?? 0) < 20 {
  103. make.width.equalTo(addMaterialBtn.snp.height)
  104. }else {
  105. make.width.equalTo(0)
  106. }
  107. make.right.lessThanOrEqualTo(exportBtn.snp.left).offset(-15)
  108. }
  109. if assets != nil, (assets?.count ?? 0) > 1 {
  110. stripSwithView.resetView(count: assets?.count ?? 0)
  111. } else if assets != nil,(assets?.count ?? 0) > 0 {
  112. // 曝光上报
  113. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_pageView, objectType: assets?.first?.mediaType == .video ? .ot_video: .ot_image, pageSource: .sp_speekPage, commonParams: commonParams())
  114. }
  115. // 已选素材上报
  116. if assets != nil ,(assets?.count ?? 0) > 0 {
  117. var materialTypeList:[String] = []
  118. assets?.forEach({ item in
  119. materialTypeList.append(item.mediaType == .video ? "video": "image")
  120. })
  121. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_pageView, objectType: .ot_addMaterialSuccess, pageSource: .sp_speekPage,extParams: ["materialTypeList":materialTypeList],commonParams: commonParams())
  122. }
  123. }
  124. // MARK: - Button action
  125. func updateAsset(_ phAssets:[PHAsset]) {
  126. recordScreenVC.fetchMaterial(phAssets)
  127. if recordScreenVC.itemModels.count >= 20 {
  128. addMaterialBtn.snp.remakeConstraints { make in
  129. make.left.equalTo(stripSwithView.snp.right).offset(-2)
  130. make.top.height.equalTo(backButton!)
  131. make.width.equalTo(0)
  132. make.right.lessThanOrEqualTo(exportBtn.snp.left).offset(-15)
  133. }
  134. }
  135. resetStripSwithView(recordScreenVC.itemModels.count)
  136. }
  137. func resetStripSwithView(_ count: Int){
  138. stripSwithView.resetView(count: count)
  139. DispatchQueue.main.async {[weak self] in
  140. guard let wself = self else { return }
  141. wself.stripSwithView.snp.updateConstraints { make in
  142. if count > 1 {
  143. make.width.equalTo(cScreenWidth).priority(.high)
  144. }else{
  145. make.width.equalTo(0).priority(.high)
  146. }
  147. }
  148. wself.stripSwithView.setNeedsLayout()
  149. wself.stripSwithView.layoutIfNeeded()
  150. }
  151. }
  152. @objc func addmaterial(){
  153. if recordScreenVC.itemModels.count >= 20{
  154. cShowHUB(superView: nil, msg: "option_tips_max20".BFLocale)
  155. return
  156. }
  157. if recordScreenVC.isNormalPlaying {
  158. recordScreenVC.pause()
  159. }
  160. let alee = BFActionSheetView()
  161. if recordScreenVC.itemModels.count > 0 && recordScreenVC.itemModels.last!.mediaType != .Camera {
  162. alee.addAction(title: "home_shoot".BFLocale, sheetStyle: .defalt) {[weak self] in
  163. guard let wself = self else { return }
  164. let phAsset = PHAsset()
  165. phAsset.title = "record.camera"
  166. wself.updateAsset([phAsset])
  167. }
  168. }
  169. alee.addAction(title: "home_choose".BFLocale, sheetStyle: .defalt) {[weak self] in
  170. guard let wself = self else { return }
  171. let vc = INPhotoVideosController()
  172. vc.maxMaterialNum = 20 - wself.recordScreenVC.itemModels.count
  173. vc.modalPresentationStyle = .fullScreen
  174. vc.nextBtnCallBack = {[weak self] phAssets in
  175. guard let wself = self else { return }
  176. wself.updateAsset(phAssets)
  177. }
  178. wself.navigationController?.present(vc, animated: true, completion: nil)
  179. }
  180. alee.addAction(title: "option_cancel".BFLocale, sheetStyle: .cancle, handler: nil)
  181. alee.show()
  182. // let alertv = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  183. // if let subview = alertv.view.subviews.first?.subviews.first?.subviews.first {
  184. // subview.backgroundColor = .black
  185. // }
  186. //
  187. // if recordScreenVC.itemModels.count > 0 && recordScreenVC.itemModels.last!.mediaType != .Camera {
  188. // let action = UIAlertAction(title: "拍摄录制", style: .default) {[weak self] action in
  189. // guard let wself = self else { return }
  190. //
  191. // let phAsset = PHAsset()
  192. // phAsset.title = "record.camera"
  193. // wself.updateAsset([phAsset])
  194. // }
  195. // alertv.addAction(action)
  196. // }
  197. //
  198. // let action1 = UIAlertAction(title: "从相册选择", style: .destructive) {[weak self] action in
  199. // guard let wself = self else { return }
  200. // let vc = INPhotoVideosController()
  201. // vc.maxMaterialNum = 20 - wself.recordScreenVC.itemModels.count
  202. // vc.modalPresentationStyle = .fullScreen
  203. // vc.nextBtnCallBack = {[weak self] phAssets in
  204. // guard let wself = self else { return }
  205. //
  206. // wself.updateAsset(phAssets)
  207. // }
  208. //
  209. // wself.navigationController?.present(vc, animated: true, completion: nil)
  210. // }
  211. // alertv.addAction(action1)
  212. //
  213. // let action2 = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  214. // alertv.addAction(action2)
  215. //
  216. // self.present(alertv, animated: true, completion: nil)
  217. }
  218. override func backBtnClick() {
  219. let alertController = UIAlertController(title: "compose_tips_exit".BFLocale,
  220. message: "", preferredStyle: .alert)
  221. let cancelAction = UIAlertAction(title: "option_not exit".BFLocale, style: .default, handler: nil)
  222. let okAction = UIAlertAction(title: "option_exit".BFLocale, style: .cancel, handler: {[weak self]
  223. action in
  224. self?.recordScreenVC.backBtnClick()
  225. // self?.super_back()
  226. self?.navigationController?.popToRootViewController(animated: true)
  227. })
  228. okAction.setValue(UIColor.red, forKey:"titleTextColor")
  229. alertController.addAction(okAction)
  230. alertController.addAction(cancelAction)
  231. self.present(alertController, animated: true, completion: nil)
  232. }
  233. private func super_back() {
  234. super.backBtnClick()
  235. }
  236. @objc func exportAction() {
  237. let count = recordScreenVC.itemModels.reduce(0, { partialResult, model in
  238. model.voiceStickers.count + partialResult
  239. })
  240. if count == 0 {
  241. cShowHUB(superView: nil, msg: "compose_tips_export".BFLocale)
  242. return
  243. }
  244. recordScreenVC.backBtnClick()
  245. let controller = INVideoExportController()
  246. controller.export.data = recordScreenVC.itemModels
  247. controller.export.originSoundVolumn = recordScreenVC.noSpeakVolume
  248. controller.export.originSoundInRecordVolumn = recordScreenVC.haveSpeakVolume
  249. controller.coverIV.image = recordScreenVC.itemModels.first?.coverImg
  250. navigationController?.pushViewController(controller, animated: true)
  251. // 点击导出按钮上报
  252. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_exportButton, pageSource: .sp_speekPage,commonParams: commonParams())
  253. }
  254. }