How to show AVplayer current play duration in UISlider
Asked Answered
M

3

5

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.

Maritamaritain answered 25/10, 2011 at 19:50 Comment(0)
F
6

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

Fregoso answered 25/10, 2011 at 21:9 Comment(2)
I am using a UIToolBar to place the UISlider, on putting the UILabel it acts like a separate UIBarButtonItem along with the glowing effect. So it doesn't gel well like in MPMoviePlayerController.Maritamaritain
Instead of putting the UISlider directly in the UIToolBar, put a UIView in the toolbar. Make the UISlider a subview of the UIView, along with two UILabels.Fregoso
S
12

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
}
Spiky answered 16/1, 2018 at 12:17 Comment(1)
I used the extension method and worked perfectly. Thank you very much. Is there an easy way to let users select the current time with slider?Brickyard
F
6

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

Fregoso answered 25/10, 2011 at 21:9 Comment(2)
I am using a UIToolBar to place the UISlider, on putting the UILabel it acts like a separate UIBarButtonItem along with the glowing effect. So it doesn't gel well like in MPMoviePlayerController.Maritamaritain
Instead of putting the UISlider directly in the UIToolBar, put a UIView in the toolbar. Make the UISlider a subview of the UIView, along with two UILabels.Fregoso
T
0
[slider setMaximumValue:[AVPlayerItem duration]];
//use NSTimer, run repeat function
-(void)updateValueSlide{
      [slider setValue:[AVPlayerItem duration]];
 }
Thermion answered 9/5, 2013 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.