xcode AVPlayer - 为 CMTime 添加秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15098004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:51:50  来源:igfitidea点击:

AVPlayer - Add Seconds to CMTime

iphoneiosxcodeavplayercmtime

提问by Lorenz W?hr

How can I add 5 seconds to my current playing Time?
Actually this is my code:

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

CMTime currentTime = music.currentTime;

I can′t use CMTimeGetSeconds() , because I need the CMTime format.

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

Thank you for your answers...

谢谢您的回答...

EDIT: How can I set a variable for CMTime?

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

回答by BlueVoodoo

Here is one way:

这是一种方法:

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

回答by tomasgatial

elegant way is using CMTimeAdd

优雅的方式是使用 CMTimeAdd

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

CMTime resultTime  = CMTimeAdd(currentTime,timeToAdd);

//then hopefully 
[music seekToTime:resultTime];

to your edit: you can create CMTime struct by these ways

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

CMTimeMake
CMTimeMakeFromDictionary
CMTimeMakeWithEpoch
CMTimeMakeWithSeconds

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

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

回答by Rudolf Adamkovi?

In Swift:

在斯威夫特:

private extension CMTime {

    func timeWithOffset(offset: NSTimeInterval) -> CMTime {

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

        return CMTimeMakeWithSeconds(secondsWithOffset, timescale)

    }

}

回答by idrougge

Swift 4, using custom operator:

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

}