0

I'm having trouble with UISlider and AVPlayer scrubbing method. Every time the method is invoked the player restarts from 0. I tried debugging and seems that the slider value is right but when I Step Over, it's set 0, thus the player restarts. This is what I've tried:

    var desiredTime = CMTimeMake(Int64(self.progressSlider.value), 1)

    AudioManager.sharedInstance.audioPlayer.seekToTime(desiredTime)

I've also tried looking at similar questions and found that it was the same when I tried their solutions.

I appreciate your help.

  • 1
    Have you set the sliders minimum to 0 and the maximum to the duration of the AVAsset that you are playing? –  Jul 29 '15 at 23:46
  • Thanks @MichaelL. This works! But how do I update the `progressSlider.value` to the `desiredTime`? Right now it stays at the same position from where I dragged it. – Enock Nshuti Jul 30 '15 at 11:55

2 Answers2

0

The use of the method wasn't right. You can look up in the Library.

var desiredTime = CMTimeMake(self.progressSlider.value *1000, 1000);
AudioManager.sharedInstance.audioPlayer.seekToTime(desiredTime)
ronan
  • 1,611
  • 13
  • 20
0

As I indicated in my comment, you need to set the sliders minimum value to 0 and its maximum value to the duration of the asset being played.

To make the slider move as the player plays, add a periodic observer to the player. For example:

CMTime interval = CMTimeMakeWithSeconds(.25,1000); [_player addPeriodicTimeObserverForInterval:interval queue:NULL usingBlock:
    (CMTime time)){ NSTimeInterval t = CMTimeGetSeconds(time);
         mySlider.value = t; }];

(You may need a weak reference to self to access mySlider.) The time parameter to the block is the player's current time.

When the user slides, use seekToTime to set the players position.

One caveat: although this works, the increments of the slider will show as little jumps of the thumb. I have not figured out a simple way to make the slider move continuously - probably worth a question of its own.

  • What does the `(.25,1000)` mean in this example? This is what I tried from what you said in the comment: `let duration = Float(CMTimeGetSeconds(AudioManager.sharedInstance.audioPlayer.currentItem.asset.duration)) progressSlider.minimumValue = 0 progressSlider.maximumValue = duration var desiredTime = CMTimeMake(Int64(self.progressSlider.value), 1) AudioManager.sharedInstance.audioPlayer.seekToTime(desiredTime)`. This is working, but then where should I implement your example? – Enock Nshuti Jul 30 '15 at 15:58
  • Look at the documentation for CMTime - it explains the meaning of the parameters. 1000 is arbitrary - I use it just because it makes debugging easier. It might be better to use a number that is a multiple of 60 when you are dealing with time. As for your other question, that depends on your overall design, so I cannot answer it. I feel I have fully answered your original question. If you have more questions, please ask them as new questions. –  Jul 30 '15 at 16:11