1

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.

Community
  • 1
  • 1
senty
  • 12,385
  • 28
  • 130
  • 260
  • See the following for how to use `seekToTime`: http://stackoverflow.com/questions/31711936/avplayer-seektotime-not-working-starting-from-beginning-everytime For how to link slider value to a function (in your case, a function implementing `seekToTime`), see my answer (using `@IBAction`) here: http://stackoverflow.com/questions/34459115/cannot-convert-value-of-type-float-to-expected-argument-cgfloat/34459130#34459130 – dfrib Dec 25 '15 at 16:59
  • What are the steps I should take? 1) Set the protocol in ViewController as I showed in the question. 2) Add the delegate like `public class Player: UIViewController, UserSetsTheTime {` (is this right?) 3) [Put that](http://stackoverflow.com/a/31714293/4705339) as you said, but where to place it? If you can add an answer, I can accept it.. – senty Dec 25 '15 at 17:05
  • I'm sorry, I can only hint you to using the threads above to implement your current time manipulator, however not give you and exhaustive answer, hence posting as comments. Perhaps someone else can help you with a more thorough answer. – dfrib Dec 25 '15 at 17:13
  • Hopefully, someone posts a detailed answer because I am so confused adapting it to Player demo. thanks for your comments tho – senty Dec 25 '15 at 17:34
  • @senty can you share your code via dropbox so we can help? theres not much to go on here. – Jeremie D Jan 05 '16 at 14:43
  • @Jeremie [Uploaded on GoogleDrive](https://drive.google.com/file/d/0BxWHl6OLoWW6SGpBRU5wcDhNYmM/view?usp=sharing). I have just achieved one more step 2 min ago and I can manipulate the SeekToTime now but it looks quite dodgy. (Please run the project, tap on screen when video is playing and try to change the time). Also, just now, I've also found [this on StackOverflow](http://stackoverflow.com/questions/22717167/how-to-enable-tap-and-slide-in-a-uislider), which looks interesting but on Obj-c unfortunately. – senty Jan 05 '16 at 14:55

2 Answers2

3

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)
    }

aimak
  • 540
  • 2
  • 12
0

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.

senty
  • 12,385
  • 28
  • 130
  • 260