Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer - Add Seconds to CMTime

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

CMTime currentTime = music.currentTime;

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

Thank you for your answers...

EDIT: How can I set a variable for CMTime?

like image 598
Lorenz Wöhr Avatar asked Feb 26 '13 19:02

Lorenz Wöhr


4 Answers

Here is one way:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
like image 133
BlueVoodoo Avatar answered Nov 06 '22 17:11

BlueVoodoo


elegant way is using 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

CMTimeMake
CMTimeMakeFromDictionary
CMTimeMakeWithEpoch
CMTimeMakeWithSeconds

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

like image 37
tomasgatial Avatar answered Nov 06 '22 17:11

tomasgatial


In Swift:

private extension CMTime {

    func timeWithOffset(offset: TimeInterval) -> CMTime {

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

        return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)

    }

}
like image 5
Rudolf Adamkovič Avatar answered Nov 06 '22 16:11

Rudolf Adamkovič


Swift 4, using custom operator:

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

}
like image 1
idrougge Avatar answered Nov 06 '22 17:11

idrougge