ios - AVAudioplayer如何让当前时间自动更新

标签 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

谢谢。

最佳答案

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 上的文本

希望现在一切都清楚

关于ios - AVAudioplayer如何让当前时间自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43120424/

相关文章:

ios - 在 Swift 中选择照片时出现 "Array index out of range"

ios - Iphone - 当每个单元格高度是动态的时,何时计算 tableview 的 heightForRowAtIndexPath?

swift - 从 NSCalendar.dateFromComponents 获取奇怪的日期

ios - 设置 ImageView 的位置

ios - 从 Info.plist 中删除启动屏幕文件会缩放 View

iphone - GMGridView 选择被覆盖

具有多行项目的 iOS 选择器控件

ios - 使用核心数据的最佳实践

ios - Storyboard无法打开

xcode - swift : Trying to start MapKit location updates without prompting for location authorization