是否可以在AVPlayer中获得播放时间和总播放时间?如果是,我该怎么做?
答案 0 :(得分:142)
您可以使用currentItem
属性访问当前播放的项目:
AVPlayerItem *currentItem = yourAVPlayer.currentItem;
然后您可以轻松获得所需的时间值
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
答案 1 :(得分:22)
_audioPlayer = [self playerWithAudio:_audio];
_observer =
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2)
queue:dispatch_get_main_queue()
usingBlock:^(CMTime time)
{
_progress = CMTimeGetSeconds(time);
}];
答案 2 :(得分:11)
Swift 3
let currentTime:Double = player.currentItem.currentTime().seconds
您可以通过访问seconds
的{{1}}属性来获取当前时间的秒数。这将返回表示时间秒的currentTime()
。然后,您可以使用此值构建可读时间以呈现给您的用户。
首先,提供一个方法来返回您将向用户显示的Double
的时间变量:
H:mm:ss
接下来,将上面检索到的值转换为可读字符串的方法:
func getHoursMinutesSecondsFrom(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int) {
let secs = Int(seconds)
let hours = secs / 3600
let minutes = (secs % 3600) / 60
let seconds = (secs % 3600) % 60
return (hours, minutes, seconds)
}
现在,使用之前的计算更新UI:
func formatTimeFor(seconds: Double) -> String {
let result = getHoursMinutesSecondsFrom(seconds: seconds)
let hoursString = "\(result.hours)"
var minutesString = "\(result.minutes)"
if minutesString.characters.count == 1 {
minutesString = "0\(result.minutes)"
}
var secondsString = "\(result.seconds)"
if secondsString.characters.count == 1 {
secondsString = "0\(result.seconds)"
}
var time = "\(hoursString):"
if result.hours >= 1 {
time.append("\(minutesString):\(secondsString)")
}
else {
time = "\(minutesString):\(secondsString)"
}
return time
}
答案 3 :(得分:8)
使用Swift 4.2,使用它;
let currentPlayer = AVPlayer()
if let currentItem = currentPlayer.currentItem {
let duration = currentItem.asset.duration
}
let currentTime = currentPlayer.currentTime()
答案 4 :(得分:5)
AVPlayerItem *currentItem = player.currentItem;
NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime);
NSLog(@" Capturing Time :%f ",currentTime);
答案 5 :(得分:4)
夫特:
let currentItem = yourAVPlayer.currentItem
let duration = currentItem.asset.duration
var currentTime = currentItem.asset.currentTime
答案 6 :(得分:3)
雨燕4
self.playerItem = AVPlayerItem(url: videoUrl!)
self.player = AVPlayer(playerItem: self.playerItem)
self.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main, using: { (time) in
if self.player!.currentItem?.status == .readyToPlay {
let currentTime = CMTimeGetSeconds(self.player!.currentTime())
let secs = Int(currentTime)
self.timeLabel.text = NSString(format: "%02d:%02d", secs/60, secs%60) as String//"\(secs/60):\(secs%60)"
})
}
答案 7 :(得分:1)
快速 5+
可以直接查询播放器,找到当前正在播放的AVPlayerItem的当前时间。 时间存储在 CMTime Struct 中,以便于转换为各种比例,例如 10th of sec、100th of a sec 等 在大多数情况下,我们需要以秒为单位表示时间,因此以下内容将向您展示您想要的
let currentTimeInSecs = CMTimeGetSeconds(player.currentTime())
答案 8 :(得分:0)
Swift 4.2:
let currentItem = yourAVPlayer.currentItem
let duration = currentItem.asset.duration
let currentTime = currentItem.currentTime()
答案 9 :(得分:0)
迅速5: 如果您想要平滑的进度条,则Timer.scheduledTimer似乎比addPeriodicTimeObserver更好
static public var currenTime = 0.0
static public var currenTimeString = "00:00"
Timer.scheduledTimer(withTimeInterval: 1/60, repeats: true) { timer in
if self.player!.currentItem?.status == .readyToPlay {
let timeElapsed = CMTimeGetSeconds(self.player!.currentTime())
let secs = Int(timeElapsed)
self.currenTime = timeElapsed
self.currenTimeString = NSString(format: "%02d:%02d", secs/60, secs%60) as String
print("AudioPlayer TIME UPDATE: \(self.currenTime) \(self.currenTimeString)")
}
}