senty
senty

Reputation: 12847

Setting Slider Value to Set SeekToTime in AVPlayer

I am using Player library, that is using AVPlayer & AVFoundation, which is quiet convenient for my case. I successfully managed to play the video and add a slider. I set the slider's min to 0 and max to duration of the video..

At this point, in order to connect slider to current playtime, I used this answer, on StackOverflow. I setup a protocol and used addPeriodicTimeObserverForInterval, so slider is linked to the currentTime and moving as video moves along successfully.

Now, here is my problem. I want to do the reverse and make - when slider moves, set currentTime of the video (forward,backward).

I made some research and I found out that it's seekToTime that I should link the slider.value (sender.value in my case) but, I couldn't understand how to apply it to Player. How can I make the slider to control the currentTime?


I am not sure if I am right, but as far as I understand, the first step should be:

protocol UserSetsTheTimeDelegate
{
  func userSetsTheTime(result: Int)
}

class ViewController: UIViewController, PlayerDelegate {
       ...

  func sliderValueDidChange(sender:UISlider!) {
     print("value--\(sender.value)")
     self.delegate?.userSetsTheTime(sender.value)
  }
}

And add, I think I should add the UserSetsTheTimeDelegate in Player.swift, but I got confused here.


Update:

I realised that I can send the data to the Player.swift by using an instance in ViewController and adding a method in Player class. This is the first step. But now, as currentTime is getting updated (with modifying the slider) so quickly, it doesn't let me manipulate the slider value.

Upvotes: 1

Views: 4316

Answers (2)

aimak
aimak

Reputation: 540

It is preferable from an UX point of view to call the seek() method on your Player after a touchUp on your slider, rather than whenever the value changes.

As you noticed, you had poor experience with the sliderValueDidChange.

Instead, try by adding a target/action on UIControlEvents.TouchUpInside (for instance) on your slider, then only seek. The experience should be better.


    var slider : UISlider! {
        didSet {
          slider.addTarget(self, action: Selector("touchUp:"), forControlEvents: [UIControlEvents.TouchUpInside])
        }
    }

    func touchUp(sender:AnyObject?) {
        // player.seek(slider.currentValue)
    }

Upvotes: 3

senty
senty

Reputation: 12847

I solved my problem. I created an instance of Player so I was able to send the data from the view controller back to Player class. Then I simply applied seekToTime(). At this point, visually, it was seeming poor but then I noticed that I can use stop(), setCurrentTime() and then play() in order to make it look nicer.

Upvotes: 0

Related Questions