Pit*_*Pan 0 iphone time avfoundation ios swift
在我的应用程序,我用AVFoundation我称之为功能showCurrentAudioProgress()使用NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "showCurrentAudioProgress", userInfo: nil, repeats: true)在viewDidLoad。我想返回这种时间格式00:00:00,但是我有一个大问题。整个功能如下所示:
var musicPlayer = AVAudioPLayer()
func showCurrentAudioProgress() {
self.totalTimeOfAudio = self.musicPlayer.duration
self.currentTimeOfAudio = self.musicPlayer.currentTime
let progressToShow = Float(self.currentTimeOfAudio) / Float(self.totalTimeOfAudio)
self.audioProgress.progress = progressToShow
var totalTime = self.musicPlayer.duration
totalTime -= self.currentTimeOfAudio
self.currentTimeTimer.text = "\(currentTimeOfAudio)"
self.AudioDurationTime.text = "\(totalTime)"
}
Run Code Online (Sandbox Code Playgroud)
如何将我收到的时间转换AVAudioPlayer为00:00:00?
您可以使用NSDateComponentsFormatter .Positional样式和zeroFormattingBehavior .Pad来显示时间间隔,如下所示:
extension NSTimeInterval {
struct DateComponents {
static let formatterPositional: NSDateComponentsFormatter = {
let formatter = NSDateComponentsFormatter()
formatter.allowedUnits = [.Hour,.Minute,.Second]
formatter.unitsStyle = .Positional
formatter.zeroFormattingBehavior = .Pad
return formatter
}()
}
var positionalTime: String {
return DateComponents.formatterPositional.stringFromTimeInterval(self) ?? ""
}
}
print(120.positionalTime) // 0:02:00
Run Code Online (Sandbox Code Playgroud)
如果您想使用字符串格式的初始化程序,可以看一下这个答案。
| 归档时间: |
|
| 查看次数: |
900 次 |
| 最近记录: |