AVSynchronizedLayer not synchronizing animation

2019-05-27 16:03发布

问题:

I'm having issues making the animation use the AVPlayer time instead of the system time. the synchronized layer does not work properly and animations stay synchronized on the system time instead of the player time. I know the player do play. and if I pass CACurrentMediaTime() to the beginTime, the animation start right away as it should when not synchronized.

EDIT

I can see the red square in its final state since the beginning, which mean the animation has reach its end at the beginning because it is synchronized on the system time and not the AVPlayerItem time.

    // play composition live in order to modifier
    AVPlayerItem  * playerItem  = [AVPlayerItem  playerItemWithAsset:composition];
    AVPlayer      * player      = [AVPlayer      playerWithPlayerItem:playerItem];
    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player   ];
    playerLayer.frame           = [UIScreen mainScreen].bounds;

    if (!playerItem) {
        NSLog(@"playerItem empty");
    }
    // dummy time
    playerItem.forwardPlaybackEndTime = totalDuration;
    playerItem.videoComposition       = videoComposition;

    CALayer * aLayer       = [CALayer layer];
    aLayer.frame           = CGRectMake(100, 100, 60, 60);
    aLayer.backgroundColor = [UIColor redColor].CGColor;
    aLayer.opacity         = 0.f;

    CAKeyframeAnimation * keyframeAnimation2 = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    keyframeAnimation2.removedOnCompletion   = NO;
    keyframeAnimation2.beginTime             = 0.1;
    keyframeAnimation2.duration              = 4.0;
    keyframeAnimation2.fillMode              = kCAFillModeBoth;
    keyframeAnimation2.keyTimes              = @[@0.0, @1.0];
    keyframeAnimation2.values                = @[@0.f, @1.f];
    NSLog(@"%f current media time", CACurrentMediaTime());

    [aLayer addAnimation:keyframeAnimation2
                  forKey:@"opacity"];

    [self.parentLayer addSublayer:aLayer];

    AVSynchronizedLayer * synchronizedLayer =
    [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];


    synchronizedLayer.frame = [UIScreen mainScreen].bounds;
    [synchronizedLayer addSublayer:self.parentLayer];
    [playerLayer       addSublayer:synchronizedLayer];

回答1:

The solution was that AVSynchronizedLayer doesn't work on the Simulator but works fine on a device.