Is it possible get playing time and total play time in AVPlayer? If yes, how can I do this?
是否有可能在AVPlayer获得播放时间和总播放时间?如果是,我怎么做呢?
6 个解决方案
#1
120
You can access currently played item by using currentItem
property:
您可以使用当前播放的物品使用货币属性:
AVPlayerItem *currentItem = yourAVPlayer.currentItem;
Then you can easily get the requested time values
然后您可以很容易地获得所请求的时间值
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
#2
21
_audioPlayer = [self playerWithAudio:_audio];
_observer =
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2)
queue:dispatch_get_main_queue()
usingBlock:^(CMTime time)
{
_progress = CMTimeGetSeconds(time);
}];
#3
8
Swift 3
斯威夫特3
let currentTime:Double = player.currentItem.currentTime().seconds
You can get the seconds of your current time by accessing the seconds
property of the currentTime()
. This will return a Double
that represents the seconds in time. Then you can use this value to construct a readable time to present to your user.
通过访问currentTime()的seconds属性,可以获得当前时间的秒。这将返回一个表示时间秒数的双精度浮点数。然后您可以使用这个值来构造一个可读的时间来呈现给您的用户。
First, include a method to return the time variables for H:mm:ss
that you will display to the user:
首先,包含一个方法来返回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)
}
Next, a method that will convert the values you retrieved above into a readable string:
接下来,将上面检索到的值转换为可读字符串的方法:
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
}
Now, update the UI with the previous calculations:
现在,使用前面的计算更新UI:
func updateTime() {
// Access current item
if let currentItem = player.currentItem {
// Get the current time in seconds
let playhead = currentItem.currentTime().seconds
let duration = currentItem.duration.seconds
// Format seconds for human readable string
playheadLabel.text = formatTimeFor(seconds: playhead)
durationLabel.text = formatTimeFor(seconds: duration)
}
}
#4
6
With Swift 2.0, use this;
使用Swift 2.0,请使用这个;
let currentPlayerItem = AVPlayer.currentItem
let duration = currentPlayerItem?.asset.duration
var currentTime = AVPlayer.currentTime()
#5
5
AVPlayerItem *currentItem = player.currentItem;
NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime);
NSLog(@" Capturing Time :%f ",currentTime);
#6
3
Swift:
迅速:
let currentItem = yourAVPlayer.currentItem
let duration = currentItem.asset.duration
var currentTime = currentItem.asset.currentTime
#1
120
You can access currently played item by using currentItem
property:
您可以使用当前播放的物品使用货币属性:
AVPlayerItem *currentItem = yourAVPlayer.currentItem;
Then you can easily get the requested time values
然后您可以很容易地获得所请求的时间值
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
#2
21
_audioPlayer = [self playerWithAudio:_audio];
_observer =
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2)
queue:dispatch_get_main_queue()
usingBlock:^(CMTime time)
{
_progress = CMTimeGetSeconds(time);
}];
#3
8
Swift 3
斯威夫特3
let currentTime:Double = player.currentItem.currentTime().seconds
You can get the seconds of your current time by accessing the seconds
property of the currentTime()
. This will return a Double
that represents the seconds in time. Then you can use this value to construct a readable time to present to your user.
通过访问currentTime()的seconds属性,可以获得当前时间的秒。这将返回一个表示时间秒数的双精度浮点数。然后您可以使用这个值来构造一个可读的时间来呈现给您的用户。
First, include a method to return the time variables for H:mm:ss
that you will display to the user:
首先,包含一个方法来返回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)
}
Next, a method that will convert the values you retrieved above into a readable string:
接下来,将上面检索到的值转换为可读字符串的方法:
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
}
Now, update the UI with the previous calculations:
现在,使用前面的计算更新UI:
func updateTime() {
// Access current item
if let currentItem = player.currentItem {
// Get the current time in seconds
let playhead = currentItem.currentTime().seconds
let duration = currentItem.duration.seconds
// Format seconds for human readable string
playheadLabel.text = formatTimeFor(seconds: playhead)
durationLabel.text = formatTimeFor(seconds: duration)
}
}
#4
6
With Swift 2.0, use this;
使用Swift 2.0,请使用这个;
let currentPlayerItem = AVPlayer.currentItem
let duration = currentPlayerItem?.asset.duration
var currentTime = AVPlayer.currentTime()
#5
5
AVPlayerItem *currentItem = player.currentItem;
NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime);
NSLog(@" Capturing Time :%f ",currentTime);
#6
3
Swift:
迅速:
let currentItem = yourAVPlayer.currentItem
let duration = currentItem.asset.duration
var currentTime = currentItem.asset.currentTime