如何为当前的播放时间添加5秒?实际上这是我的代码:
CMTime currentTime = music.currentTime;
我不能使用CMTimeGetSeconds(),因为我需要CMTime格式 .
谢谢您的回答...
编辑:如何为CMTime设置变量?
这是一种方式:
CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
优雅的方式是使用 CMTimeAdd
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
在Swift中:
private extension CMTime { func timeWithOffset(offset: NSTimeInterval) -> CMTime { let seconds = CMTimeGetSeconds(self) let secondsWithOffset = seconds + offset return CMTimeMakeWithSeconds(secondsWithOffset, 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) } }
4 回答
这是一种方式:
优雅的方式是使用
CMTimeAdd
编辑:你可以通过这些方式创建CMTime结构
更多@:https://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html
在Swift中:
Swift 4,使用自定义运算符: