![](/img/trans.png)
[英]objectiveC/CMTime - convert AVPlayer.duration to milliseconds
[英]AVPlayer - Add Seconds to CMTime
如何將 5 秒添加到我當前的播放時間?
其實這是我的代碼:
CMTime currentTime = music.currentTime;
我不能使用 CMTimeGetSeconds() ,因為我需要 CMTime 格式。
謝謝您的回答...
編輯:如何為 CMTime 設置變量?
這是一種方法:
CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
優雅的方式是使用CMTimeAdd
CMTime currentTime = music.currentTime;
CMTime timeToAdd = CMTimeMakeWithSeconds(5,1);
CMTime resultTime = CMTimeAdd(currentTime,timeToAdd);
//then hopefully
[music seekToTime:resultTime];
到您的編輯:您可以通過這些方式創建 CMTime 結構
CMTimeMake
CMTimeMakeFromDictionary
CMTimeMakeWithEpoch
CMTimeMakeWithSeconds
更多@: https : //developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html
在斯威夫特:
private extension CMTime {
func timeWithOffset(offset: TimeInterval) -> CMTime {
let seconds = CMTimeGetSeconds(self)
let secondsWithOffset = seconds + offset
return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)
}
}
Swift 4,使用自定義運算符:
extension CMTime {
static func + (lhs: CMTime, rhs: TimeInterval) -> CMTime {
return CMTime(seconds: lhs.seconds + rhs,
preferredTimescale: lhs.timescale)
}
static func += (lhs: inout CMTime, rhs: TimeInterval) {
lhs = CMTime(seconds: lhs.seconds + rhs,
preferredTimescale: lhs.timescale)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.