我该如何将当前播放时间增加5秒?
这是我的代码:
我无法使用CMTimeGetSeconds(),因为我需要CMTime格式。 谢谢您的回答… 编辑:如何为CMTime设置变量?
这是我的代码:
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
该链接是苹果官方文档中关于CMTime的参考资料。在Swift中:
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)
}
}