我正在尝试使用
NSTimer
获取 AVPlayer
视频的 currentTime()
。但是,使用以下代码时,我总是返回 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
}