如何在AVAudioplayer中自动更新当前时间

时间:2017-03-30 14:13:09

标签: ios swift xcode avaudioplayer playback

我想知道:如何在AVAudioplayer中自动更新currentTime。 我用这个代码

    let currentTime1 = Int((player1?.currentTime)!)
    let minutes = currentTime1/60
    let seconds = currentTime1 - minutes * 60
    curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

感谢。

1 个答案:

答案 0 :(得分:0)

func startRecording() -> Void {
        ....
        .... 
     let aSelector : Selector = #selector(CLASSNAME.updateTime)
     timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector,userInfo: nil, repeats: true)
     startTime = NSDate.timeIntervalSinceReferenceDate()

}


func updateTime() {
        let currentTime = NSDate.timeIntervalSinceReferenceDate()
        var elapsedTime: NSTimeInterval = currentTime - startTime
        let minutes = UInt8(elapsedTime / 60.0)
        elapsedTime -= (NSTimeInterval(minutes) * 60)
        let seconds = UInt8(elapsedTime)
        elapsedTime -= NSTimeInterval(seconds)
        let strMinutes = String(format: "%02d", minutes)
        let strSeconds = String(format: "%02d", seconds)
        YOUR-LABEL.text = "\(strMinutes):\(strSeconds)"

    }

现在当您调用startRecording()函数时,它将开始更新YOUR-LABEL上的文本

希望现在很清楚