20

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?

Lorenz Wöhr
  • 855
  • 3
  • 14
  • 22

4 Answers4

33

Here is one way:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37
  • When I replace the '+ 5' with '- 5' it is not working...Do you know whats wrong? – Lorenz Wöhr Feb 26 '13 at 20:15
  • What is the current time in seconds? Perhaps the number goes negative? – BlueVoodoo Feb 26 '13 at 20:16
  • 1
    This is my code: CMTime currentTime = music.currentTime; float currentTimeInt = CMTimeGetSeconds(currentTime); if (currentTimeInt > 5) { CMTime newTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) - 5, music.currentTime.timescale); [music seekToTime:newTime]; } else { NSLog(@"error"); } – Lorenz Wöhr Feb 26 '13 at 20:19
  • ...EDIT: Missed your if statement. Let me check again. Was it working with + 5 but not working with - 5? – BlueVoodoo Feb 26 '13 at 20:23
  • The value doesn´t change whit the minus. I left the if statement out – Lorenz Wöhr Feb 26 '13 at 20:25
  • I see nothing wrong with your code. You'll get an error in your log if currentTimeInt is less or equal to zero. You sure this is not the issue? – BlueVoodoo Feb 26 '13 at 20:28
  • No, so what I mean is that if your currentTime is 5 seconds or less you will trigger your else block which spits out an error message in the log even though you don't actually have an error. The correct way would be to do the if statement without the else block. – BlueVoodoo Feb 26 '13 at 20:44
  • Now the value changes :) but I can´t use it with seekToTime: (nothing happens when pressed). Do you know why? – Lorenz Wöhr Feb 27 '13 at 13:08
  • Not from the top of my head. I suggest posting a new question for that. Good luck. – BlueVoodoo Feb 27 '13 at 13:10
24

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

Crashalot
  • 33,605
  • 61
  • 269
  • 439
tomasgatial
  • 235
  • 1
  • 5
5

In Swift:

private extension CMTime {

    func timeWithOffset(offset: TimeInterval) -> CMTime {

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

        return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)

    }

}
Community
  • 1
  • 1
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
1

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

}
idrougge
  • 633
  • 5
  • 12