0

As far as I understand, for passing data from a Class to a UIViewController, I used protocols & delegate, and it works like charm. What I did:

In Player class, I added:

public protocol PlayerDelegate {
   func playerPlaybacksTimer(NSString: String)
}

public convenience init() {
   player.addPeriodicTimeObserverForInterval(CMTimeMake(1, 100), queue: dispatch_get_main_queue()) {
        [unowned self] time in
        let timeString = String(format: "%02.2f", CMTimeGetSeconds(time))

        self.delegate?.playerPlaybacksTimer(timeString)
    }
}

and then I added the protocol & protocol method in UIViewController:

class ViewController: UIViewController, PlayerDelegate {

  func playerPlaybacksTimer(currentTime: String) {
    durationSlider.value = Float(currentTime)!
    print(currentTime)
  }  
}

Until here, it works nicely and I can get the current time of the player. However, now, I am trying to do the reverse, so send data from ViewController to Class. (To make what I want to achieve clear, using above approach, I assigned the current playing time of Player to a UISlider and the slider's value is changing as the video goes on)


At this point, I want to make the user to be able to change the UISlider value and manipulate/change the current playing time of the video. As far as I understand, I should use seekToTime but I am completely lost on how to achieve that.

Should I set another protocol from the ViewController to the Class? What is the way to achieve that the UISlider's value is assigned to the current value?

At least, please guide me on the first step I should take to pass data from the VC to the Class? I assume once I get the slider's value in the Player class, I can try playing with the SeekToTime approach to understand and apply.


P.S: I am using the demo project of Player (which is a quiet straight forward library) that uses AVPlayer & AVFoundation

P.S 2: I asked another question on similar topic and opened a bounty here. Please also check there, for more detailed explanation on my issue (and possibly get a 50 points bounty).

Community
  • 1
  • 1
senty
  • 12,385
  • 28
  • 130
  • 260
  • I would expect the `ViewController` to already have a reference to the `Player` instance since the delegate has to get set somehow. What does your code for setting the player delegate look like? – Phillip Mills Jan 05 '16 at 13:09
  • Should I create a protocol above the ViewController like this `protocol UserSetsTheTimeDelegate { func userSetsTheTime(result: Int) }` and call the method on Player? Like the one I showed [here](http://stackoverflow.com/questions/34464643/setting-slider-value-to-set-seektotime-in-avplayer)? If so, what is the next step to do? – senty Jan 05 '16 at 13:14
  • The purpose of a protocol is to be able to substitute different kinds of objects. If your view controller is going to sometimes have a player getting the time information and sometimes sending it to a completely different thing, the protocol makes sense. Otherwise, just call the player method. – Phillip Mills Jan 05 '16 at 13:19
  • Please let me make it clear. The view controller is receiving the time information all the time (through the delegate with `self.delegate?.playerPlaybacksTimer(timeString)`). Now, for the other way around (so letting the slider to affect the current time), (as far as I understand,) I need to send data from the view controller to the Player class. How can I do that? The only way comes to my mind is creating a protocol above the view controller class (as I showed in my previous comment). Is that right for the first step? – senty Jan 05 '16 at 13:25
  • To rephrase my previous comment: Why a delegate? Why not just send the data to the Player instance (not class!)? – Phillip Mills Jan 05 '16 at 14:48
  • Yes, I just used instance and it worked. Didn't quite understand the purpose of instance clearly before. Thanks! – senty Jan 05 '16 at 14:57

0 Answers0