2011-06-13 11 views
11

Estoy usando AVPlayer para reproducir mi video usando el deslizador y algunos botones. Aquí están mis métodos para avanzar y retroceder usando los botones.AVPlayer Video SeekToTime

-(IBAction)MoveForward 
{ 
    //int value = timeSlider.value*36000 + 10; 
    //CMTime newTime = CMTimeMakeWithSeconds(value, playspeed); 
    //CMTime newTime = CMTimeMake(value,(playspeed*timeSlider.maximumValue)); 

    CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value, playspeed); 
    newTime.value += 60; 
    [player seekToTime: newTime]; 
} 

-(IBAction)MoveBackward 
{ 
    CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value-1, playspeed); 
    [player seekToTime: newTime]; 
} 

Mi problema al respecto es que el tiempo para Buscar no funciona correctamente. Que navegue al siguiente fotograma basado en segundos. Necesito mover el próximo cuadro minuciosamente. Ayudame ...

Respuesta

22

Realmente no entiendo tu código, realmente no necesitas métodos separados para avanzar y retroceder, puedes usar el mismo para ambos. Tengo un reproductor de películas AVPlayer en funcionamiento, te mostraré cómo hice la parte del control deslizante.

-(IBAction)sliding:(id)sender{ 

     CMTime newTime = CMTimeMakeWithSeconds(seeker.value, 1); 
     [self.player seekToTime:newTime]; 
    } 

    -(void)setSlider{ 

     sliderTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES]retain]; 
     self.seeker.maximumValue = [self durationInSeconds]; 
     [seeker addTarget:self action:@selector(sliding:) forControlEvents:UIControlEventValueChanged]; 
     seeker.minimumValue = 0.0; 
     seeker.continuous = YES; 
    } 

    - (void)updateSlider { 

     self.seeker.maximumValue = [self durationInSeconds]; 
     self.seeker.value = [self currentTimeInSeconds]; 
    } 

    - (Float64)durationInSeconds { 
      Float64 dur = CMTimeGetSeconds(duration); 
     return dur; 
    } 


    - (Float64)currentTimeInSeconds { 
      Float64 dur = CMTimeGetSeconds([self.player currentTime]); 
     return dur; 
     } 

Y eso es todo, hay dos aspectos críticos en este código, en primer lugar, la propiedad duración devuelve una variable CMTime, hay que convertirlo a un flotador, también, este devuelve el número prima de segundos, usted tiene para convertirlo a h: mm: ss si desea mostrar las etiquetas de tiempo. En segundo lugar, el método updateSlider se activa mediante un temporizador cada segundo. Buena suerte.

+0

que no trabaja para me..i m que se reproduce audio con avpaler –

+0

hi ... en su código, lo que es Buscador? –

4

El siguiente fragmento de código que funcionó para mí:

CMTime videoLength = self.mPlayer.currentItem.asset.duration; // Gets the video duration 
float videoLengthInSeconds = videoLength.value/videoLength.timescale; // Transfers the CMTime duration into seconds 

[self.mPlayer seekToTime:CMTimeMakeWithSeconds(videoLengthInSeconds * [slider value], 1) 
        completionHandler:^(BOOL finished) 
      { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        isSeeking = NO; 
        // Do some stuff 
       }); 
      }]; 
+1

¡Funciona como el encanto! –

+1

'CMTime' también tiene una propiedad de conveniencia' segundos '(entre otros), por lo que no necesita hacer el cálculo usted mismo si no lo desea. – LucasTizma

+0

Lo encontré útil .. – NSPratik

Cuestiones relacionadas