AVPlayer视频currentTime始终返回0

时间:2016-04-17 02:46:49

标签: ios swift avplayer

我正在尝试使用currentTime()AVPlayer视频上获取NSTimer。使用以下代码,我总是返回0.0作为视频的当前时间。我做错了什么?

    var player: AVPlayer!

    @IBOutlet weak var playerContainer: UIView!

    var t1: Float!
    var t2: Float!
    var currentSecs: Float!

    override func viewDidLoad() {
        super.viewDidLoad()

        player = AVPlayer(URL: NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)

        let playerLayer: AVPlayerLayer = AVPlayerLayer(player: self.player)
        playerLayer.frame = self.playerContainer.frame
        self.view.layer.addSublayer(playerLayer)

        player?.play()

        t1 = Float(self.player.currentTime().value)
        t2 = Float(self.player.currentTime().timescale)
        currentSecs = t1 / t2

        var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(TheaterViewController.timeUpdate), userInfo: nil, repeats: true)

    }

    func timeUpdate() {

        print(currentSecs) // Only prints 0.0

    }

1 个答案:

答案 0 :(得分:1)

想出来。 t1t2仅在viewDidLoad中设置了一次。我将它们都移到updateTime并且它有效。 -.-