2010-10-19 11 views
31

Me gustaría hacer un UISlider (depurador) para mi AVPlayer. Pero como este no es un AVAudioPlayer, no tiene una duración incorporada. ¿Alguna sugerencia sobre cómo crear el control deslizante para avanzar, rebobinar y avanzar la reproducción?¿Cómo obtener la duración de AVPlayer (no AVAudioPlayer)?

He leído el documento en AVPlayer, tiene una función en seekToTime o seekToTime: toleranceAntes de: toleranceAfter :. Realmente no lo entiendo ¿Esta sería la respuesta para mi control deslizante? AVPlayer también tiene addPeriodicTimeObserverForInterval: queue: usingBlock :, ¿esto es para obtener la duración de mi pista? ¿Puede alguien darme un ejemplo sobre cómo implementar este código? No soy fanático de la documentación de Apple. Parece muy difícil de entender.

Respuesta

100
self.player.currentItem.asset.duration 

¡Lo tengo!

+0

Wow, ¡Gracias! Fue frustración porque compila self.player.currentItem.duration. –

+15

CMTimeGetSeconds (código anterior) para el valor flotante :) – coolcool1994

+1

Para los videos largos no funciona en algunos casos. Devuelve 0.00 ¿Alguna idea? – jose920405

34

cabeceras

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

código

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

marcos

AVFoundation 
CoreMedia 
+4

a veces devuelve un NaN – 0oneo

9

A partir de iOS 4.3, puede utilizar la slig htly más corto:

self.player.currentItem.duration; 
+0

obtener el CMTime de .currentItem.asset.duration toma 3-4 segundos? ¿Cómo resolver este problema? ¿Todavía uso el hilo pero no lo resuelvo? –

3

anotó en StitchedStreamPlayer

Debe utilizar 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

En este ejemplo AVPlayer es la instancia AVPlayer.

he construido un control de vídeo que utiliza la siguiente:

para posicionar el deslizador usar algo como esto para obtener el porcentaje de cabeza lectora a través de la película, que tendrá que despedir a esta función repetidamente. Así que me gustaría ejecutar la función como:

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; 

} 

Luego de actualizar el video Me gustaría hacer algo como:

- (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

para SWIFT para conseguir duración en segundos

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

obtener el CMTime de .currentItem.asset.duration toma 3-4 segundos? ¿Cómo resolver este problema? ¿Todavía uso el hilo pero no lo resuelvo? –

Cuestiones relacionadas