AVPlayer - 向CMTime添加秒数

Lor*_*öhr 16 iphone xcode ios avplayer cmtime

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

CMTime currentTime = music.currentTime;
Run Code Online (Sandbox Code Playgroud)

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

谢谢您的回答...

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

Blu*_*doo 26

这是一种方式:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
Run Code Online (Sandbox Code Playgroud)


小智 22

优雅的方式正在使用 CMTimeAdd

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

CMTime resultTime  = CMTimeAdd(currentTime,timeToAdd);

//then hopefully 
[music seekToTime:resultTime];
Run Code Online (Sandbox Code Playgroud)

编辑:你可以通过这些方式创建CMTime结构

CMTimeMake
CMTimeMakeFromDictionary
CMTimeMakeWithEpoch
CMTimeMakeWithSeconds
Run Code Online (Sandbox Code Playgroud)

更多@:https: //developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html


Rud*_*vič 5

在斯威夫特中:

private extension CMTime {

    func timeWithOffset(offset: TimeInterval) -> CMTime {

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

        return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)

    }

}
Run Code Online (Sandbox Code Playgroud)