LWPlayerUtils.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // LWPlayerUtils.swift
  3. // SwiftBilibili
  4. //
  5. // Created by 罗文 on 2021/3/30.
  6. // Copyright © 2021年 罗文. All rights reserved.
  7. //
  8. import UIKit
  9. import MediaPlayer
  10. /// EZPlayerState的相等判断
  11. ///
  12. /// - Parameters:
  13. /// - lhs: 左值
  14. /// - rhs: 右值
  15. /// - Returns: 比较结果
  16. public func ==(lhs: LWPlayerState, rhs: LWPlayerState) -> Bool {
  17. switch (lhs, rhs) {
  18. case (.unknown, .unknown): return true
  19. case (.readyToPlay, .readyToPlay): return true
  20. case (.buffering, .buffering): return true
  21. case (.bufferFinished, .bufferFinished): return true
  22. case (.playing, .playing): return true
  23. case (.seekingForward, .seekingForward): return true
  24. case (.seekingBackward, .seekingBackward): return true
  25. case (.pause, .pause): return true
  26. case (.stopped, .stopped): return true
  27. case (.error(let a), .error(let b)) where a == b: return true
  28. default: return false
  29. }
  30. }
  31. /// EZPlayerState的不相等判断
  32. ///
  33. /// - Parameters:
  34. /// - lhs: 左值
  35. /// - rhs: 右值
  36. /// - Returns: 比较结果
  37. public func !=(lhs: LWPlayerState, rhs: LWPlayerState) -> Bool {
  38. return !(lhs == rhs)
  39. }
  40. public class LWPlayerUtils {
  41. /// system volume ui
  42. public static let systemVolumeSlider : UISlider = {
  43. let volumeView = MPVolumeView()
  44. volumeView.showsVolumeSlider = true
  45. volumeView.showsRouteButton = false
  46. var returnSlider : UISlider!
  47. for view in volumeView.subviews {
  48. if let slider = view as? UISlider {
  49. returnSlider = slider
  50. break
  51. }
  52. }
  53. return returnSlider
  54. }()
  55. /// fotmat time
  56. ///
  57. /// - Parameters:
  58. /// - position: video current position
  59. /// - duration: video duration
  60. /// - Returns: formated time string
  61. public static func formatTime( position: TimeInterval,duration:TimeInterval) -> String{
  62. guard !position.isNaN && !duration.isNaN else{
  63. return ""
  64. }
  65. let positionHours = (Int(position) / 3600) % 60
  66. let positionMinutes = (Int(position) / 60) % 60
  67. let positionSeconds = Int(position) % 60;
  68. let durationHours = (Int(duration) / 3600) % 60
  69. let durationMinutes = (Int(duration) / 60) % 60
  70. let durationSeconds = Int(duration) % 60
  71. if(durationHours == 0){
  72. return String(format: "%02d:%02d/%02d:%02d",positionMinutes,positionSeconds,durationMinutes,durationSeconds)
  73. }
  74. return String(format: "%02d:%02d:%02d/%02d:%02d:%02d",positionHours,positionMinutes,positionSeconds,durationHours,durationMinutes,durationSeconds)
  75. }
  76. }