AVSynchronizedLayer not synchronizing animation

[亡魂溺海] 提交于 2019-12-22 12:50:36

问题


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.



来源:https://stackoverflow.com/questions/19707985/avsynchronizedlayer-not-synchronizing-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!