CameraViewFactory1.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // CameraViewFactory1.swift
  3. // Pods
  4. //
  5. // Created by jiajunzhou on 2020/4/5.
  6. //
  7. import Foundation
  8. import Flutter
  9. import UIKit
  10. import AliyunVideoSDKPro
  11. public class CameraViewFactory1 :NSObject,FlutterPlatformViewFactory{
  12. var messenger: FlutterBinaryMessenger!
  13. public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
  14. return CameraView1(withFrame: frame, viewIdentifier: viewId, arguments: args, binaryMessenger: messenger)
  15. }
  16. @objc public init(messenger: (NSObject & FlutterBinaryMessenger)?) {
  17. super.init()
  18. self.messenger = messenger
  19. }
  20. }
  21. public class CameraView1: NSObject, FlutterPlatformView,AliyunIRecorderDelegate{
  22. public func recorderDeviceAuthorization(_ status: AliyunIRecorderDeviceAuthor) {
  23. if status == AliyunIRecorderDeviceAuthor.enabled {
  24. print("sdk enabled")
  25. } else {
  26. print("sdk disabled")
  27. }
  28. }
  29. public func recorderDidStopRecording(){
  30. print("recorderDidStopRecording.....");
  31. }
  32. // 调用stopRecording停止录制后,SDK内部会执行保存视频相关操作,收到AliyunIRecorderDelegate的- (void)recorderDidStopRecording回调后才能继续执行其他操作。
  33. // startRecording和stopRecording需要成对出现,可以调用一次或多次,对应SDK内部会生成一段或多段临时视频文件。
  34. public func view() -> UIView {
  35. return self.cameraView
  36. }
  37. fileprivate var viewId: Int64!
  38. fileprivate var cameraView: UIView!
  39. fileprivate var channel: FlutterMethodChannel!
  40. fileprivate var recordPath: String?
  41. fileprivate var taskPath: String?
  42. fileprivate var recorder: AliyunIRecorder!
  43. fileprivate var composeResult: FlutterResult?
  44. //初始设置参数
  45. var videoWidth : Int!
  46. var videoHeight : Int!
  47. var fps : Int!
  48. var videoCodecs : Int!
  49. var crf : Int!
  50. var encoderFps : Int!
  51. var quality : Int!
  52. var videoBitrate : Int!
  53. var gop : Int!
  54. var first :Bool = true;
  55. public init(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?, binaryMessenger: FlutterBinaryMessenger) {
  56. super.init()
  57. self.viewId = viewId
  58. self.cameraView = UIView()
  59. self.cameraView.frame = UIScreen.main.bounds
  60. self.channel = FlutterMethodChannel(name: "flutter_ali_camera", binaryMessenger: binaryMessenger)
  61. self.channel.setMethodCallHandler({
  62. [weak self]
  63. (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
  64. if let this = self {
  65. this.onMethodCall(call: call, result: result)
  66. }
  67. })
  68. }
  69. func onMethodCall(call: FlutterMethodCall, result: @escaping FlutterResult) {
  70. if(call.method=="initializeSdk"){
  71. result(true);
  72. }
  73. else if(call.method=="create"){
  74. let args = call.arguments as? [String: Any]
  75. var dics = args!["recordOption"] as? Dictionary<String, Int>
  76. self.videoHeight = dics!["videoHeight"]
  77. self.videoWidth = dics!["videoWidth"]
  78. self.fps = dics!["fps"]
  79. self.videoCodecs = dics!["videoCodecs"]
  80. self.crf = dics!["crf"]
  81. self.encoderFps = dics!["encoderFps"]
  82. self.quality = dics!["quality"]
  83. self.videoBitrate = dics!["videoBitrate"]
  84. self.gop = dics!["gop"]
  85. // recorder = AliyunIRecorder.init(delegate: self, videoSize: CGSize(width: 720 , height: 1080))
  86. // recorder?.preview = self.cameraView
  87. // recorder?.taskPath = self.taskPath
  88. // recorder?.clipManager?.maxDuration = 0
  89. result(true);
  90. }else if(call.method=="startPreview"){
  91. if(first){
  92. first=false;
  93. recorder = AliyunIRecorder.init(delegate: self, videoSize: CGSize(width: videoWidth , height: videoHeight))
  94. recorder?.preview = self.cameraView
  95. recorder?.taskPath = self.taskPath
  96. recorder?.clipManager?.maxDuration = 0
  97. recorder?.clipManager?.deleteAllPart()
  98. recorder.beautifyStatus = true
  99. }
  100. recorder?.startPreview(withPositon: AliyunIRecorderCameraPosition.front)
  101. }
  102. else if(call.method=="stopPreview"){
  103. onStop();
  104. result(true);
  105. }
  106. else if(call.method=="setBeauty"){
  107. let args = call.arguments as? Dictionary<String, Int>
  108. let level = args!["level"] as! Int
  109. recorder.beautifyValue = Int32(level)
  110. }
  111. else if(call.method=="switchCamera"){
  112. recorder.switchCameraPosition();
  113. result(true);
  114. }
  115. else if(call.method=="setFilter"){
  116. //未验证
  117. let args = call.arguments as? Dictionary<String, String>
  118. let path = args?["path"]
  119. if path == nil || path?.count == 0 {
  120. // delete filter
  121. recorder?.deleteFilter()
  122. return
  123. }
  124. recorder?.apply(AliyunEffectFilter.init(file: path))
  125. }
  126. else if(call.method=="startRecord"){
  127. let args = call.arguments as? Dictionary<String, Int>
  128. let maxDuration = args!["max"] as! Int
  129. var fileName = args!["recordPath"] as! String
  130. print("fileNamefileNamefileName=="+fileName)
  131. recorder?.outputPath = fileName
  132. recorder?.clipManager?.maxDuration = CGFloat(Float(maxDuration) / 1000.0)
  133. recorder?.startRecording()
  134. }
  135. else if(call.method=="startCompose"){
  136. }
  137. else if(call.method=="recordUpdate"){
  138. }
  139. else if(call.method=="onDestroy"){
  140. }
  141. else {
  142. result(FlutterMethodNotImplemented)
  143. }
  144. }
  145. private func onStop() {
  146. recorder?.stopRecording()
  147. recorder?.stopPreview()
  148. }
  149. }