AVPlayer - Add Seconds to CMTime
Asked Answered
P

4

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?

Polyhydroxy answered 26/2, 2013 at 19:50 Comment(0)
B
33

Here is one way:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
Botvinnik answered 26/2, 2013 at 19:56 Comment(9)
When I replace the '+ 5' with '- 5' it is not working...Do you know whats wrong?Nostoc
What is the current time in seconds? Perhaps the number goes negative?Botvinnik
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"); }Nostoc
...EDIT: Missed your if statement. Let me check again. Was it working with + 5 but not working with - 5?Botvinnik
The value doesn´t change whit the minus. I left the if statement outNostoc
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?Botvinnik
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.Botvinnik
Now the value changes :) but I can´t use it with seekToTime: (nothing happens when pressed). Do you know why?Nostoc
Not from the top of my head. I suggest posting a new question for that. Good luck.Botvinnik
L
25

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

Lomasi answered 27/2, 2013 at 23:20 Comment(3)
'AVPlayer' may not respond to 'setCurrentTime'Nostoc
Sending 'CMTime' to parameter of incompatible type 'NSTimeInterval' (aka 'double')Nostoc
maybe you should use [music seekToTime: resultTime]Lomasi
O
5

In Swift:

private extension CMTime {

    func timeWithOffset(offset: TimeInterval) -> CMTime {

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

        return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)

    }

}
Orville answered 31/3, 2015 at 13:18 Comment(0)
H
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)
    }

}
Hazzard answered 16/10, 2018 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.