2013-02-26 79 views
13

如何添加5秒到我目前的播放时间?
其实这是我的代码:AVPlayer - 添加秒到CMTime

CMTime currentTime = music.currentTime; 

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

谢谢你的答案...

编辑:我怎样才能设置一个变量CMTime?

回答

21

这里有一种方法:

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

完美!谢谢:) – 2013-02-26 20:03:10

+0

当我用' - 5'替换'+ 5'时,它不工作...你知道最新错误吗? – 2013-02-26 20:15:01

+0

现在的时间是几秒钟?也许这个数字是负数? – BlueVoodoo 2013-02-26 20:16:17

18

优雅的方式是使用CMTimeAdd

CMTime currentTime = music.currentTime; 
CMTime timeToAdd = CMTimeMakeWithSeconds(5,1); 

CMTime resultTime = CMTimeAdd(currentTime,timeToAdd); 

//then hopefully 
[music seekToTime:resultTime]; 

您的编辑: 您可以通过以下方式

CMTimeMake 
CMTimeMakeFromDictionary 
CMTimeMakeWithEpoch 
CMTimeMakeWithSeconds 

更创造CMTime结构@ : https://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html

+0

'AVPlayer'可能不会响应'setCurrentTime' – 2013-03-01 15:42:09

+0

将'CMTime'发送到不兼容类型'NSTimeInterval'(又名'double')的参数 – 2013-03-01 15:43:16

+0

也许您应该使用[music seekToTime:resultTime] – tomasgatial 2013-03-02 08:14:33

2

在斯威夫特:

private extension CMTime { 

    func timeWithOffset(offset: NSTimeInterval) -> CMTime { 

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

     return CMTimeMakeWithSeconds(secondsWithOffset, timescale) 

    } 

}