Getting Current Time of a Video (Player - Swift)

送分小仙女□ 提交于 2019-12-05 20:57:01

I just run you GitHub project and i fixed it by following code:

In your Player.swift Class.

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

            print("time is \(timeString)")
            self.delegate?.playerPlaybackstimer(timeString)

            }

That Above method added in public convenience init() { funtion so that will be look like:

 public convenience init() {
        self.init(nibName: nil, bundle: nil)
        self.player = AVPlayer()




        self.player.actionAtItemEnd = .Pause
        self.player.addObserver(self, forKeyPath: PlayerRateKey, options: ([NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Old]) , context: &PlayerObserverContext)

        self.playbackLoops = false
        self.playbackFreezesAtEnd = false
        self.playbackState = .Stopped
        self.bufferingState = .Unknown


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

            print("time is \(timeString)")
            self.delegate?.playerPlaybackstimer(timeString)

            }

    }

After that i make a Delegate method in public protocol PlayerDelegate: classthat will be look like:

public protocol PlayerDelegate: class {
    func playerReady(player: Player)
    func playerPlaybackStateDidChange(player: Player)
    func playerBufferingStateDidChange(player: Player)

    func playerPlaybackWillStartFromBeginning(player: Player)
    func playerPlaybackDidEnd(player: Player)

     func playerPlaybackstimer(NSString: String)
}

Now go in your ViewController.swift class and add the following function:

func playerPlaybackstimer(NSString: String) {

        print("currunt time \(NSString)")
    }

Now run your project and enjoy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!