LNVAPWrapView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import UIKit
  2. @objcMembers
  3. public final class LNVAPWrapView: UIView {
  4. private var playerView: LNVAPPlayerView?
  5. private var delegateBridge = LNWrapDelegateBridge()
  6. public weak var delegate: LNVAPWrapPlaybackDelegate?
  7. public weak var legacyDelegate: LNVAPWrapLegacyPlaybackDelegate?
  8. public var contentModeOption: LNVAPWrapContentMode = .scaleToFill {
  9. didSet {
  10. applyContentModeIfPossible()
  11. }
  12. }
  13. @objc(lnWrapContentMode)
  14. public var lnWrapContentMode: Int {
  15. get { contentModeOption.legacyRawValue }
  16. set { contentModeOption = LNVAPWrapContentMode(rawValue: newValue) ?? .scaleToFill }
  17. }
  18. public var autoDestroyAfterFinish: Bool = true
  19. @objc(autoDestoryAfterFinish)
  20. public var autoDestoryAfterFinish: Bool {
  21. get { autoDestroyAfterFinish }
  22. set { autoDestroyAfterFinish = newValue }
  23. }
  24. public override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. delegateBridge.owner = self
  27. }
  28. public required init?(coder: NSCoder) {
  29. super.init(coder: coder)
  30. delegateBridge.owner = self
  31. }
  32. public override func layoutSubviews() {
  33. super.layoutSubviews()
  34. if contentModeOption == .scaleToFill {
  35. playerView?.frame = bounds
  36. }
  37. }
  38. @objc(lnPlayWithFilePath:repeatCount:)
  39. public func lnPlay(filePath: String, repeatCount: Int) {
  40. delegateBridge.lastPlayedFrameIndex = 0
  41. let player = initPlayerViewIfNeed()
  42. player.lnPlay(filePath: filePath, repeatCount: repeatCount)
  43. }
  44. @objc(lnPlayWithFilePath:)
  45. public func lnPlay(filePath: String) {
  46. lnPlay(filePath: filePath, repeatCount: 0)
  47. }
  48. @objc(lnPlayDeprecatedWithFilePath:fps:blendMode:repeatCount:delegate:)
  49. public func lnPlayDeprecated(filePath: String,
  50. fps: Int,
  51. blendMode: Int,
  52. repeatCount: Int,
  53. delegate: LNVAPWrapPlaybackDelegate?) {
  54. _ = blendMode
  55. self.delegate = delegate
  56. delegateBridge.lastPlayedFrameIndex = 0
  57. let player = initPlayerViewIfNeed()
  58. player.fps = fps
  59. player.lnPlay(filePath: filePath, repeatCount: repeatCount)
  60. }
  61. @objc(lnStop)
  62. public func lnStop() {
  63. playerView?.lnStop()
  64. }
  65. @objc(lnPause)
  66. public func lnPause() {
  67. playerView?.lnPause()
  68. }
  69. @objc(lnResume)
  70. public func lnResume() {
  71. playerView?.lnResume()
  72. }
  73. @objc(lnSetMute:)
  74. public func lnSetMute(_ mute: Bool) {
  75. let player = initPlayerViewIfNeed()
  76. player.lnSetMute(mute)
  77. }
  78. @objc(lnAddVapTapGesture:)
  79. public func lnAddVapTapGesture(_ handler: @escaping LNVAPGestureEventBlock) {
  80. let player = initPlayerViewIfNeed()
  81. player.lnAddVapTapGesture(handler)
  82. }
  83. @objc(lnAddVapGesture:callback:)
  84. public func lnAddVapGesture(_ gestureRecognizer: UIGestureRecognizer, callback: @escaping LNVAPGestureEventBlock) {
  85. let player = initPlayerViewIfNeed()
  86. player.lnAddVapGesture(gestureRecognizer, callback: callback)
  87. }
  88. @objc(addVapTapGesture:)
  89. public func addVapTapGesture(_ handler: @escaping LNVAPGestureEventBlock) {
  90. lnAddVapTapGesture(handler)
  91. }
  92. @objc(addVapGesture:callback:)
  93. public func addVapGesture(_ gestureRecognizer: UIGestureRecognizer, callback: @escaping LNVAPGestureEventBlock) {
  94. lnAddVapGesture(gestureRecognizer, callback: callback)
  95. }
  96. public override func hitTest(_ hitPoint: CGPoint, with event: UIEvent?) -> UIView? {
  97. if !isUserInteractionEnabled || isHidden || alpha < 0.01 {
  98. return nil
  99. }
  100. if point(inside: hitPoint, with: event) {
  101. for subview in subviews.reversed() {
  102. let convertedPoint = convert(hitPoint, to: subview)
  103. if let hitView = subview.hitTest(convertedPoint, with: event) {
  104. return hitView
  105. }
  106. }
  107. return nil
  108. }
  109. return nil
  110. }
  111. fileprivate func shouldStart(with config: LNVAPConfigModel) -> Bool {
  112. applyContentMode(with: config)
  113. if let allow = delegate?.lnWrapViewShouldStart?(self, config: config) {
  114. return allow
  115. }
  116. return legacyDelegate?.vapWrap_viewshouldStartPlayMP4?(self, config: config) ?? true
  117. }
  118. fileprivate func notifyStart() {
  119. delegate?.lnWrapViewDidStart?(self)
  120. legacyDelegate?.vapWrap_viewDidStartPlayMP4?(self)
  121. }
  122. fileprivate func notifyPlay(_ frame: LNMP4AnimatedImageFrame) {
  123. delegate?.lnWrapViewDidPlay?(self, frame: frame)
  124. legacyDelegate?.vapWrap_viewDidPlayMP4AtFrame?(frame, view: self)
  125. }
  126. fileprivate func notifyFinish(_ totalFrameCount: Int) {
  127. let computedCount = max(totalFrameCount, delegateBridge.lastPlayedFrameIndex + 1)
  128. delegate?.lnWrapViewDidFinish?(self, totalFrameCount: computedCount)
  129. legacyDelegate?.vapWrap_viewDidFinishPlayMP4?(computedCount, view: self)
  130. }
  131. fileprivate func notifyStop() {
  132. delegate?.lnWrapViewDidStop?(self)
  133. let lastFrameIndex = delegateBridge.lastPlayedFrameIndex
  134. legacyDelegate?.vapWrap_viewDidStopPlayMP4?(lastFrameIndex, view: self)
  135. delegateBridge.lastPlayedFrameIndex = 0
  136. DispatchQueue.main.async { [weak self] in
  137. guard let self else { return }
  138. if self.autoDestroyAfterFinish {
  139. self.playerView?.removeFromSuperview()
  140. self.playerView = nil
  141. }
  142. }
  143. }
  144. fileprivate func notifyFail(_ error: NSError) {
  145. delegate?.lnWrapViewDidFail?(self, error: error)
  146. legacyDelegate?.vapWrap_viewDidFailPlayMP4?(error)
  147. }
  148. fileprivate func contentForTag(_ tag: String, resource: LNVAPSourceInfo) -> String? {
  149. if let text = delegate?.lnWrapViewContent?(forTag: tag, resource: resource) {
  150. return text
  151. }
  152. return legacyDelegate?.vapWrapview_contentForVapTag?(tag, resource: resource)
  153. }
  154. fileprivate func loadImage(withURL url: String, context: NSDictionary, completion: @escaping LNVAPImageCompletion) {
  155. if let handler = delegate?.lnWrapViewLoadImage {
  156. handler(url, context, completion)
  157. return
  158. }
  159. if (legacyDelegate as AnyObject?)?.responds(to: #selector(LNVAPWrapLegacyPlaybackDelegate.vapWrapView_loadVapImage(withURL:context:completion:))) == true {
  160. legacyDelegate?.vapWrapView_loadVapImage?(withURL: url, context: context, completion: completion)
  161. return
  162. }
  163. completion(nil, nil, url)
  164. }
  165. private func initPlayerViewIfNeed() -> LNVAPPlayerView {
  166. if let playerView {
  167. return playerView
  168. }
  169. let view = LNVAPPlayerView(frame: bounds)
  170. view.delegate = delegateBridge
  171. addSubview(view)
  172. playerView = view
  173. applyContentModeIfPossible()
  174. return view
  175. }
  176. private func applyContentModeIfPossible() {
  177. guard let playerView else { return }
  178. guard let config = delegateBridge.lastConfig else {
  179. playerView.frame = bounds
  180. return
  181. }
  182. applyContentMode(with: config)
  183. }
  184. private func applyContentMode(with config: LNVAPConfigModel) {
  185. guard let playerView else { return }
  186. guard let info = config.info, info.size.width > 0, info.size.height > 0 else {
  187. playerView.frame = bounds
  188. return
  189. }
  190. let layoutWidth = bounds.width
  191. let layoutHeight = bounds.height
  192. guard layoutWidth > 0, layoutHeight > 0 else { return }
  193. let layoutRatio = layoutWidth / layoutHeight
  194. let videoRatio = info.size.width / info.size.height
  195. switch contentModeOption {
  196. case .scaleToFill:
  197. playerView.frame = bounds
  198. case .aspectFit:
  199. let realSize: CGSize
  200. if layoutRatio < videoRatio {
  201. let width = layoutWidth
  202. realSize = CGSize(width: width, height: width / videoRatio)
  203. } else {
  204. let height = layoutHeight
  205. realSize = CGSize(width: height * videoRatio, height: height)
  206. }
  207. playerView.frame = CGRect(origin: .zero, size: realSize)
  208. playerView.center = CGPoint(x: bounds.midX, y: bounds.midY)
  209. case .aspectFill:
  210. let realSize: CGSize
  211. if layoutRatio > videoRatio {
  212. let width = layoutWidth
  213. realSize = CGSize(width: width, height: width / videoRatio)
  214. } else {
  215. let height = layoutHeight
  216. realSize = CGSize(width: height * videoRatio, height: height)
  217. }
  218. playerView.frame = CGRect(origin: .zero, size: realSize)
  219. playerView.center = CGPoint(x: bounds.midX, y: bounds.midY)
  220. }
  221. }
  222. }
  223. private final class LNWrapDelegateBridge: NSObject, LNVAPPlaybackDelegate {
  224. weak var owner: LNVAPWrapView?
  225. var lastConfig: LNVAPConfigModel?
  226. var lastPlayedFrameIndex: Int = 0
  227. func lnPlayerShouldStart(_ playerView: LNVAPPlayerView, config: LNVAPConfigModel) -> Bool {
  228. lastConfig = config
  229. return owner?.shouldStart(with: config) ?? true
  230. }
  231. func lnPlayerDidStart(_ playerView: LNVAPPlayerView) {
  232. owner?.notifyStart()
  233. }
  234. func lnPlayerDidPlay(_ playerView: LNVAPPlayerView, frame: LNMP4AnimatedImageFrame) {
  235. lastPlayedFrameIndex = frame.frameIndex
  236. owner?.notifyPlay(frame)
  237. }
  238. func lnPlayerDidStop(_ playerView: LNVAPPlayerView) {
  239. owner?.notifyStop()
  240. }
  241. func lnPlayerDidFinish(_ playerView: LNVAPPlayerView, totalFrameCount: Int) {
  242. owner?.notifyFinish(totalFrameCount)
  243. }
  244. func lnPlayerDidFail(_ playerView: LNVAPPlayerView, error: NSError) {
  245. owner?.notifyFail(error)
  246. }
  247. func lnPlayerContent(forTag tag: String, resource: LNVAPSourceInfo) -> String? {
  248. owner?.contentForTag(tag, resource: resource)
  249. }
  250. func lnPlayerLoadImage(withURL url: String, context: NSDictionary, completion: @escaping LNVAPImageCompletion) {
  251. owner?.loadImage(withURL: url, context: context, completion: completion)
  252. }
  253. }