2013-02-26 13 views
13

Come aggiungere 5 secondi al tempo di riproduzione corrente?
In realtà questo è il mio codice:AVPlayer - Aggiungi secondi a CMTime

CMTime currentTime = music.currentTime; 

non posso usare CMTimeGetSeconds(), perché ho bisogno il formato CMTime.

Grazie per le vostre risposte ...

EDIT: Come posso impostare una variabile per CMTime?

risposta

21

Ecco un modo:

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

Perfect !!! Grazie :) –

+0

Quando sostituisco il '+ 5' con '- 5' non funziona ... Sai che cosa non va? –

+0

Qual è l'ora corrente in secondi? Forse il numero diventa negativo? – BlueVoodoo

18

modo elegante sta usando CMTimeAdd

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

CMTime resultTime = CMTimeAdd(currentTime,timeToAdd); 

//then hopefully 
[music seekToTime:resultTime]; 

per la modifica: è possibile creare CMTime struct da questi modi

CMTimeMake 
CMTimeMakeFromDictionary 
CMTimeMakeWithEpoch 
CMTimeMakeWithSeconds 

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

+0

'AVPlayer' potrebbe non rispondere a 'setCurrentTime' –

+0

Invio 'CMTime' a parametro di tipo incompatibile 'NSTimeInterval' (aka 'double') –

+0

forse dovresti usare [musica seekToTime: resultTime] – tomasgatial

2

In Swift:

private extension CMTime { 

    func timeWithOffset(offset: NSTimeInterval) -> CMTime { 

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

     return CMTimeMakeWithSeconds(secondsWithOffset, timescale) 

    } 

}