我正在尝试使用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
}
答案 0 :(得分:1)
想出来。 t1
和t2
仅在viewDidLoad
中设置了一次。我将它们都移到updateTime
并且它有效。 -.-