2011-05-09 7 views
12

He intentado producir una prueba de concepto que conecte un video con un objeto de animación (esencialmente, estoy "rotoscopando" un mapa de bits seleccionado por el usuario en un video enlatado en ubicaciones específicas). Después de ver todo el material de Apple disponible sobre Core Animation (WWDC10, etc.) y AVSync..Layer (código de ejemplo de Apple WWDC10, código de muestra de doc), simplemente no hay un ejemplo de código aislado lo suficientemente claro como para derivar cómo se implementó este modo de reproducción correctamente.Configuración de AVSynchonizedLayer

Usando CA, tengo un activo de video que se carga en un CALayer, y una animación que se adjunta a un mapa de bits, y luego se conecta a otro CALayer. Creé un AVSynchronizedLayer y agregué ambos objetos como subcapas.

Cuando este árbol se agrega a la propiedad de capa de UIView, esperaba que la reproducción de la animación se ejecutara en sincronización con el video. En su lugar, solo veo el video reproduciéndose, y la animación nunca se ejecuta: aquí están las piezas relevantes de mi código que todo reside dentro de un único Controlador de Vista (llamado dentro de ViewDidLoad).

Video101.m4v = M4V de vídeo simple, gerald.png = mapa de bits

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Video101" withExtension:@"m4v"]; 
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; 
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; 

AVPlayer *player = [[AVPlayer playerWithPlayerItem:playerItem]retain]; 
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; 
playerLayer.frame = CGRectMake(0, 0, 1024, 768); 
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

CALayer *gerald = [CALayer layer]; 
gerald.frame = CGRectMake(0, 0, 120, 120); 
gerald.contents = (id) [UIImage imageNamed:@"gerald.png"].CGImage; 

[self.view.layer addSublayer:playerLayer]; 
[self.view.layer insertSublayer:gerald above:playerLayer]; 


CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
anim.duration = 18.0; 

anim.values = [NSArray arrayWithObjects: 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(480.0, 206.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(10.0, 760.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], 
       [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], 
       [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], nil]; 


anim.keyTimes = [NSArray arrayWithObjects: 
       [NSNumber numberWithFloat:0.02], 
       [NSNumber numberWithFloat:0.06], 
       [NSNumber numberWithFloat:0.09], 
       [NSNumber numberWithFloat:0.1], 
       [NSNumber numberWithFloat:0.12], 
       [NSNumber numberWithFloat:0.2], 
       [NSNumber numberWithFloat:0.25], 
       [NSNumber numberWithFloat:0.32], 
       [NSNumber numberWithFloat:0.38], 
       [NSNumber numberWithFloat:0.49], 
       [NSNumber numberWithFloat:0.53], 
       [NSNumber numberWithFloat:0.59], 
       [NSNumber numberWithFloat:0.63], 
       [NSNumber numberWithFloat:0.69], 
       [NSNumber numberWithFloat:0.78], 
       [NSNumber numberWithFloat:0.81], 
       [NSNumber numberWithFloat:0.91], 
       [NSNumber numberWithFloat:1.0], nil]; 


AVSynchronizedLayer *syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem]; 
[syncLayer addSublayer:gerald]; 
[gerald addAnimation:anim forKey:@"transform.position"]; 

[self.view.layer addSublayer:syncLayer]; 

[player play]; 

¿Cuál es el formato correcto o manera de implementar AVSynchronizedLayer, por lo que tanto la animación y reproducción de vídeo juntos?

Respuesta

12

Usted tiene el CAKeyframeAnimation configurado correctamente a excepción de un bit: Según la WWDC "edición de medios con la Fundación AV", conferencia, es necesario establecer la propiedad beginTime de la animación a un valor distinto de cero.

Zero beginTime se traduce automáticamente a CACurrentMediaTime(). Use un pequeño número distinto de cero: por ejemplo, 1e-100 o -1 sexies-100 (AVCoreAnimationBeginTimeAtZero)

creo que si se agrega anim.beginTime = AVCoreAnimationBeginTimeAtZero; encontrará la animación comienza justo cuando el vídeo comienza a reproducirse.

+2

Clavado. ¡Rock - thx por la ayuda! –

+0

puedo agregar retraso a la animación haciendo algo como: AVCoreAnimationBeginTimeAtZero + 1 o CACurrentMediaTime() + 1 – Nil

Cuestiones relacionadas