首页 文章

AVPlayer - 向CMTime添加秒数

提问于
浏览
15

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

CMTime currentTime = music.currentTime;

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

谢谢您的回答...

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

4 回答

  • 2

    这是一种方式:

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

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

  • 24

    在Swift中:

    private extension CMTime {
    
        func timeWithOffset(offset: NSTimeInterval) -> CMTime {
    
            let seconds = CMTimeGetSeconds(self)
            let secondsWithOffset = seconds + offset
    
            return CMTimeMakeWithSeconds(secondsWithOffset, timescale)
    
        }
    
    }
    
  • 0

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

相关问题