AVPlayer - 向CMTime添加秒数

时间:2013-02-26 19:50:58

标签: iphone ios xcode avplayer cmtime

如何为当前的播放时间添加5秒? 实际上这是我的代码:

CMTime currentTime = music.currentTime;

我不能使用CMTimeGetSeconds(),因为我需要CMTime格式。

感谢您的回答......

编辑:如何为CMTime设置变量?

4 个答案:

答案 0 :(得分:24)

这是一种方式:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);

答案 1 :(得分:20)

优雅的方式是使用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

答案 2 :(得分:2)

在斯威夫特:

private extension CMTime {

    func timeWithOffset(offset: NSTimeInterval) -> CMTime {

        let seconds = CMTimeGetSeconds(self)
        let secondsWithOffset = seconds + offset

        return CMTimeMakeWithSeconds(secondsWithOffset, timescale)

    }

}

答案 3 :(得分:0)

快速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)
    }

}