VideoExportController.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // VideoExportController.swift
  3. // BFRecordScreenKit_Example
  4. //
  5. // Created by 胡志强 on 2021/11/25.
  6. // Copyright © 2021 CocoaPods. All rights reserved.
  7. //
  8. import Foundation
  9. import BFRecordScreenKit
  10. import BFUIKit
  11. import UIKit
  12. import AVFoundation
  13. class VideoExportController: BFBaseViewController{
  14. var videoAsset : AVURLAsset?
  15. lazy var progressView : UIView = {
  16. let v = UIView(frame: CGRect(x: 0, y: navHeadImageView!.bottomY, width: 0, height: 10))
  17. v.backgroundColor = .red
  18. return v
  19. }()
  20. lazy var progressL : UILabel = {
  21. let la = UILabel(frame: CGRect(x: 0, y: navHeadImageView!.bottomY, width: cScreenWidth, height: 10))
  22. la.textColor = .white
  23. la.textAlignment = .center
  24. la.text = "0%"
  25. la.font = UIFont.systemFont(ofSize: 8)
  26. return la
  27. }()
  28. lazy var export : BFRecordExport = {
  29. let export = BFRecordExport()
  30. export.progress = {[weak self] progress in
  31. self?.progressView.frame = CGRect(x: 0, y: self?.navHeadImageView?.bottomY ?? 0, width: (cScreenWidth) * CGFloat(progress), height: 10)
  32. self?.progressL.text = String(format: "%d%%", Int(progress*100))
  33. }
  34. export.exportCompletion = {[weak self] (error, url) in
  35. guard let strongSelf = self else {
  36. return
  37. }
  38. if let fileUrl = url {
  39. DispatchQueue.main.async {
  40. let item = AVPlayerItem(url: fileUrl)
  41. let avplayer = AVPlayer(playerItem: item)
  42. let playerLayer = AVPlayerLayer(player: avplayer)
  43. playerLayer.frame = CGRect(x: 10, y: strongSelf.progressView.bottomY, width: cScreenWidth - 20, height: cScreenHeigth - strongSelf.progressView.bottomY - 10)
  44. strongSelf.view.layer.addSublayer(playerLayer)
  45. avplayer.play()
  46. }
  47. }
  48. }
  49. return export
  50. }()
  51. override func viewWillAppear(_ animated: Bool) {
  52. super.viewWillAppear(animated)
  53. showNavigation()
  54. }
  55. override func backBtnClick() {
  56. export.cancelExport()
  57. super.backBtnClick()
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. view.backgroundColor = .black
  62. navHeadImageView?.backgroundColor = .black
  63. leftButton(image: nil, imageName: nil, tintColor: UIColor.white)
  64. let backV = UIView(frame: CGRect(x: 0, y: navHeadImageView!.bottomY, width: cScreenWidth, height: 10))
  65. backV.backgroundColor = .gray
  66. view.addSubview(backV)
  67. view.addSubview(progressView)
  68. view.addSubview(progressL)
  69. export.startExprot()
  70. }
  71. }