TSAIListVideoPlayerVC.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // TSVideoPlayerVC.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/17.
  6. //
  7. import UIKit
  8. import AVKit
  9. import SnapKit
  10. class TSAIListVideoPlayerVC: UIViewController {
  11. // MARK: - Properties
  12. private var player: AVPlayer?
  13. private var playerLayer: AVPlayerLayer?
  14. private var timeObserverToken: Any?
  15. private var isPlaying = false{
  16. didSet{
  17. playPauseButton.setImage(isPlaying ? .videoPause : .videoPlay, for: .normal)
  18. }
  19. }
  20. private let videoURL: URL
  21. public var isRunloppPlay:Bool = false
  22. // MARK: - UI Components
  23. private lazy var playerContainerView: UIView = {
  24. let view = UIView()
  25. view.backgroundColor = .clear
  26. return view
  27. }()
  28. private lazy var playPauseButton: TSUIExpandedTouchButton = {
  29. let button = TSUIExpandedTouchButton()
  30. button.setImage(.videoPlay, for: .normal)
  31. button.tintColor = .white
  32. button.addTarget(self, action: #selector(playPauseTapped), for: .touchUpInside)
  33. return button
  34. }()
  35. private lazy var progressSlider: TSProgressSlider = {
  36. let slider = TSProgressSlider()
  37. slider.minimumTrackTintColor = UIColor.themeColor
  38. slider.maximumTrackTintColor = .white.withAlphaComponent(0.2)
  39. slider.setThumbImage(UIImage.circle(diameter: 10, color: .white), for: .normal)
  40. slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
  41. slider.addTarget(self, action: #selector(sliderTouchEnded(_:)), for: .touchUpInside)
  42. slider.addTarget(self, action: #selector(sliderTouchEnded(_:)), for: .touchUpOutside)
  43. return slider
  44. }()
  45. private lazy var currentTimeLabel: UILabel = {
  46. let label = UILabel.createLabel(text: "00:00",font:.font(size: 12),textColor: .white)
  47. return label
  48. }()
  49. private lazy var durationLabel: UILabel = {
  50. let label = UILabel.createLabel(text: "00:00",font:.font(size: 12),textColor: .white)
  51. return label
  52. }()
  53. private lazy var controlsContainerView: UIView = {
  54. let view = UIView()
  55. return view
  56. }()
  57. // MARK: - Initialization
  58. init(videoURL: URL) {
  59. self.videoURL = videoURL
  60. super.init(nibName: nil, bundle: nil)
  61. }
  62. required init?(coder: NSCoder) {
  63. fatalError("init(coder:) has not been implemented")
  64. }
  65. // MARK: - Lifecycle
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. setupUI()
  69. setupPlayer()
  70. dealThings()
  71. }
  72. func dealThings() {
  73. // 监听应用生命周期事件
  74. NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: .main) { _ in
  75. self.playPause()
  76. self.setControlsView(isHidden: false)
  77. }
  78. NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { _ in }
  79. }
  80. override func viewDidLayoutSubviews() {
  81. super.viewDidLayoutSubviews()
  82. playerLayer?.frame = playerContainerView.bounds
  83. }
  84. deinit {
  85. removePeriodicTimeObserver()
  86. }
  87. @objc private func clickBgView() {
  88. setControlsView(isHidden: !controlsContainerView.isHidden)
  89. }
  90. func setControlsView(isHidden:Bool) {
  91. playPauseButton.isHidden = isHidden
  92. controlsContainerView.isHidden = isHidden
  93. }
  94. // MARK: - Setup
  95. private func setupUI() {
  96. view.backgroundColor = .clear
  97. playerContainerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickBgView)))
  98. view.addSubview(playerContainerView)
  99. playerContainerView.snp.makeConstraints { make in
  100. make.edges.equalToSuperview()
  101. }
  102. playerContainerView.addSubview(playPauseButton)
  103. playPauseButton.snp.makeConstraints { make in
  104. make.centerX.equalToSuperview()
  105. make.centerY.equalToSuperview()//.offset(-50)
  106. make.width.height.equalTo(56)
  107. }
  108. playerContainerView.addSubview(controlsContainerView)
  109. controlsContainerView.snp.makeConstraints { make in
  110. make.leading.trailing.equalTo(0)
  111. make.bottom.equalTo(-80-k_Height_safeAreaInsetsBottom())
  112. }
  113. controlsContainerView.addSubview(progressSlider)
  114. controlsContainerView.addSubview(currentTimeLabel)
  115. controlsContainerView.addSubview(durationLabel)
  116. progressSlider.snp.makeConstraints { make in
  117. make.left.equalTo(16)
  118. make.right.equalTo(-16)
  119. make.top.equalTo(0)
  120. make.height.equalTo(10)
  121. }
  122. let label = UILabel.createLabel(text: "/",font: .font(size: 11),textColor: .white)
  123. controlsContainerView.addSubview(label)
  124. label.snp.makeConstraints { make in
  125. make.top.equalTo(progressSlider.snp.bottom).offset(6)
  126. make.centerX.equalToSuperview()
  127. make.height.equalTo(13)
  128. make.bottom.equalToSuperview()
  129. }
  130. currentTimeLabel.snp.makeConstraints { make in
  131. make.height.equalTo(13)
  132. make.centerY.equalTo(label)
  133. make.right.equalTo(label.snp.left)
  134. }
  135. durationLabel.snp.makeConstraints { make in
  136. make.height.equalTo(13)
  137. make.centerY.equalTo(label)
  138. make.left.equalTo(label.snp.right)
  139. }
  140. }
  141. private func setupPlayer() {
  142. player = AVPlayer(url: videoURL)
  143. playerLayer = AVPlayerLayer(player: player)
  144. playerLayer?.videoGravity = .resizeAspect
  145. if let playerLayer = playerLayer {
  146. playerContainerView.layer.insertSublayer(playerLayer, at: 0)
  147. }
  148. // Add time observer to update progress
  149. addPeriodicTimeObserver()
  150. // Observe when the video ends
  151. NotificationCenter.default.addObserver(
  152. self,
  153. selector: #selector(playerDidFinishPlaying),
  154. name: .AVPlayerItemDidPlayToEndTime,
  155. object: player?.currentItem
  156. )
  157. // Get video duration
  158. let duration = player?.currentItem?.asset.duration.seconds ?? 0
  159. durationLabel.text = formatTime(seconds: Float(duration))
  160. }
  161. func setControlsBottom(bottem:CGFloat){
  162. controlsContainerView.snp.updateConstraints { make in
  163. make.bottom.equalTo(bottem)
  164. }
  165. }
  166. func runloppPlay() {
  167. self.isRunloppPlay = true
  168. setControlsView(isHidden: true)
  169. playPlay()
  170. }
  171. // MARK: - Player Controls
  172. @objc private func playPauseTapped() {
  173. if isPlaying {
  174. playPause()
  175. } else {
  176. playPlay()
  177. }
  178. }
  179. @objc func playPlay() {
  180. player?.play()
  181. isPlaying = true
  182. }
  183. @objc func playPause() {
  184. player?.pause()
  185. isPlaying = false
  186. }
  187. @objc private func playerDidFinishPlaying() {
  188. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1){
  189. self.playerDidFinish()
  190. }
  191. }
  192. func playerDidFinish() {
  193. player?.seek(to: CMTime.zero)
  194. isPlaying = false
  195. progressSlider.value = 0
  196. currentTimeLabel.text = "00:00"
  197. if isRunloppPlay {
  198. playPlay()
  199. }
  200. }
  201. // MARK: - Progress Slider
  202. @objc private func sliderValueChanged(_ sender: UISlider) {
  203. playPause()
  204. guard let duration = player?.currentItem?.duration else { return }
  205. let totalSeconds = CMTimeGetSeconds(duration)
  206. let value = Float64(sender.value) * totalSeconds
  207. let seekTime = CMTime(value: Int64(value), timescale: 1)
  208. currentTimeLabel.text = formatTime(seconds: Float(value))
  209. }
  210. @objc private func sliderTouchEnded(_ sender: UISlider) {
  211. guard let duration = player?.currentItem?.duration else { return }
  212. let totalSeconds = CMTimeGetSeconds(duration)
  213. let value = Float64(sender.value) * totalSeconds
  214. let seekTime = CMTime(value: Int64(value), timescale: 1)
  215. player?.seek(to: seekTime, toleranceBefore: .zero, toleranceAfter: .zero)
  216. playPlay()
  217. }
  218. // MARK: - Time Observer
  219. private func addPeriodicTimeObserver() {
  220. let interval = CMTime(seconds: 0.05, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
  221. timeObserverToken = player?.addPeriodicTimeObserver(
  222. forInterval: interval,
  223. queue: .main
  224. ) { [weak self] time in
  225. guard let self = self else { return }
  226. let timeElapsed = Float(time.seconds)
  227. if let duration = self.player?.currentItem?.duration {
  228. let durationSeconds = Float(CMTimeGetSeconds(duration))
  229. self.progressSlider.value = Float(timeElapsed / durationSeconds)
  230. self.currentTimeLabel.text = self.formatTime(seconds: timeElapsed)
  231. }
  232. }
  233. }
  234. private func removePeriodicTimeObserver() {
  235. if let token = timeObserverToken {
  236. player?.removeTimeObserver(token)
  237. timeObserverToken = nil
  238. }
  239. }
  240. // MARK: - Helper Methods
  241. private func formatTime(seconds: Float) -> String {
  242. let minutes = Int(seconds) / 60
  243. let seconds = Int(seconds) % 60
  244. return String(format: "%02d:%02d", minutes, seconds)
  245. }
  246. }