AVPlayer+LWPlayer.swift 852 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // AVPlayer+LWPlayer.swift
  3. // SwiftBilibili
  4. //
  5. // Created by 罗文 on 2021/3/28.
  6. // Copyright © 2021年 罗文. All rights reserved.
  7. //
  8. import AVFoundation
  9. public extension AVPlayer {
  10. // 观看了的时长(不包括暂停等)
  11. var durationWatched: TimeInterval {
  12. var duration: TimeInterval = 0
  13. if let events = self.currentItem?.accessLog()?.events {
  14. for event in events {
  15. duration += event.durationWatched
  16. }
  17. }
  18. return duration
  19. }
  20. // 总时长
  21. var duration: TimeInterval? {
  22. if let duration = self.currentItem?.duration {
  23. return CMTimeGetSeconds(duration)
  24. }
  25. return nil
  26. }
  27. // 播放进度
  28. var currentTime: TimeInterval? {
  29. return CMTimeGetSeconds(self.currentTime())
  30. }
  31. }