如何在UISlider中显示AVplayer当前播放时长

发布于 2024-12-12 10:20:38 字数 140 浏览 0 评论 0原文

我正在使用自定义 UISlider 在 AVPlayer 中播放视频时实现擦除。尝试找出在 UISlider 两端显示当前播放时间和剩余持续时间的最佳方式,就像通常在 MPMoviePlayerController 中显示的那样。

任何帮助表示赞赏。

I am using a custom UISlider to implement the scrubbing while playing a video in AVPlayer. Trying to figure out the best way to show the current play time and the remaining duration on the respective ends of the UISlider as it usually is shown in MPMoviePlayerController.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好久不见√ 2024-12-19 10:20:38

Swift 4.x

let player = AVPlayer()
player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
        if let duration = player.currentItem?.duration {
          let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
          let progress = (time/duration)
          if progress > targetProgress {
              print(progress)
             //Update slider value
          }
        }
    })

extension AVPlayer {
    func addProgressObserver(action:@escaping ((Double) -> Void)) -> Any {
        return self.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
            if let duration = self.currentItem?.duration {
                let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
                let progress = (time/duration)
                action(progress)
            }
        })
    }
}

使用

let player = AVPlayer()
player.addProgressObserver { progress in
   //Update slider value
}

Swift 4.x

let player = AVPlayer()
player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
        if let duration = player.currentItem?.duration {
          let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
          let progress = (time/duration)
          if progress > targetProgress {
              print(progress)
             //Update slider value
          }
        }
    })

or

extension AVPlayer {
    func addProgressObserver(action:@escaping ((Double) -> Void)) -> Any {
        return self.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
            if let duration = self.currentItem?.duration {
                let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
                let progress = (time/duration)
                action(progress)
            }
        })
    }
}

Use

let player = AVPlayer()
player.addProgressObserver { progress in
   //Update slider value
}
死开点丶别碍眼 2024-12-19 10:20:38

在每一端放置一个 UILabel。使用 -[AVPlayer addPeriodicTimeObserverForInterval:queue:usingBlock:] 更新它们。使用 -[AVPlayer currentTime]-[AVPlayerItemuration] 计算剩余时间。

Put a UILabel at each end. Update them using -[AVPlayer addPeriodicTimeObserverForInterval:queue:usingBlock:]. Compute the time remaining using -[AVPlayer currentTime] and -[AVPlayerItem duration].

江挽川 2024-12-19 10:20:38
[slider setMaximumValue:[AVPlayerItem duration]];
//use NSTimer, run repeat function
-(void)updateValueSlide{
      [slider setValue:[AVPlayerItem duration]];
 }
[slider setMaximumValue:[AVPlayerItem duration]];
//use NSTimer, run repeat function
-(void)updateValueSlide{
      [slider setValue:[AVPlayerItem duration]];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文