2010-10-19 8 views
31

Mi piacerebbe creare un UISlider (scrubber) per il mio AVPlayer. Ma dal momento che questo non è un AVAudioPlayer, non ha una durata incorporata. Qualche suggerimento su come creare lo Slider per l'avanzamento veloce, il riavvolgimento e l'avanzamento della riproduzione?Come ottenere la durata da AVPlayer (non AVAudioPlayer)?

Ho letto il documento su AVPlayer, ha un built in seekToTime o seekToTime: toleranceBefore: toleranceAfter :. Non lo capisco davvero. Questa sarebbe la risposta per il mio cursore? AVPlayer ha anche addPeriodicTimeObserverForInterval: queue: usingBlock :, è questo per ottenere la durata della mia traccia? Qualcuno può darmi un esempio su come implementare questo codice? Non sono un fan della documentazione di Apple. Sembra molto difficile da capire.

risposta

100
self.player.currentItem.asset.duration 

Trovato!

+0

Wow, Grazie! Era frustrante perché compila self.player.currentItem.duration. –

+15

CMTimeGetSeconds (aboveCode) per il valore float :) – coolcool1994

+1

Per i video lunghi, in alcuni casi i video non funzionano. Ritorno 0,00. Qualche idea? – jose920405

34

intestazioni

#import <AVFoundation/AVPlayer.h> 
#import <AVFoundation/AVPlayerItem.h> 
#import <AVFoundation/AVAsset.h> 

codice

CMTime duration = self.player.currentItem.asset.duration; 
float seconds = CMTimeGetSeconds(duration); 
NSLog(@"duration: %.2f", seconds); 

quadri

AVFoundation 
CoreMedia 
+4

a volte restituisce un NaN – 0oneo

9

Come di iOS 4.3, è possibile utilizzare la SLIG htly più breve:

self.player.currentItem.duration; 
+0

ottenere CMTime da .currentItem.asset.duration richiede 3-4 secondi? Come risolvere questo problema? Ho ancora usato thread ma non risolto? –

3

notare dalla StitchedStreamPlayer

Si dovrebbe usare player.currentItem.duration

- (CMTime)playerItemDuration 
{ 
    AVPlayerItem *thePlayerItem = [player currentItem]; 
    if (thePlayerItem.status == AVPlayerItemStatusReadyToPlay) 
    {   
     /* 
     NOTE: 
     Because of the dynamic nature of HTTP Live Streaming Media, the best practice 
     for obtaining the duration of an AVPlayerItem object has changed in iOS 4.3. 
     Prior to iOS 4.3, you would obtain the duration of a player item by fetching 
     the value of the duration property of its associated AVAsset object. However, 
     note that for HTTP Live Streaming Media the duration of a player item during 
     any particular playback session may differ from the duration of its asset. For 
     this reason a new key-value observable duration property has been defined on 
     AVPlayerItem. 

     See the AV Foundation Release Notes for iOS 4.3 for more information. 
     */  

     return([playerItem duration]); 
    } 

    return(kCMTimeInvalid); 
} 
2

In questo esempio AVPlayer è l'istanza AVPlayer.

ho costruito un controllo video che utilizza il seguente:

per posizionare il cursore usare qualcosa di simile per ottenere la percentuale di indicatore di riproduzione attraverso il film, è necessario sparare questa funzione più volte. Quindi mi sento di eseguire la funzione come:

float scrubberBarLocation = (scrubberBgImageView.frame.size.width/100.0f) * [self moviePercentage]; 


- (float)moviePercentage { 

    CMTime t1 = [avPlayer currentTime]; 
    CMTime t2 = avPlayer.currentItem.asset.duration; 

    float myCurrentTime = CMTimeGetSeconds(t1); 
    float myDuration = CMTimeGetSeconds(t2); 

    float percent = (myCurrentTime/myDuration)*100.0f; 
    return percent; 

} 

Poi per aggiornare il video vorrei fare qualcosa di simile:

- (void)updateVideoPercent:(float)thisPercent { 

    CMTime t2 = avPlayer.currentItem.asset.duration; 
    float myDuration = CMTimeGetSeconds(t2); 

    float result = myDuration * thisPercent /100.0f; 

    //NSLog(@"this result = %f",result); // debug 

    CMTime seekTime = CMTimeMake(result, 1); 

    [avPlayer seekToTime:seekTime]; 

} 
8

Per Swift per ottenere durata in secondi

if let duration = player.currentItem?.asset.duration { 
    let seconds = CMTimeGetSeconds(duration) 
    print(seconds) 
} 
+0

ottenere CMTime da .currentItem.asset.duration richiede 3-4 secondi? Come risolvere questo problema? Ho ancora usato thread ma non risolto? –

Problemi correlati