AVSynchronizedLayer不同步动画 [英] AVSynchronizedLayer not synchronizing animation

查看:745
本文介绍了AVSynchronizedLayer不同步动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作动画使用AVPlayer时间而不是系统时间的问题。同步层不能正常工作和动画保持上的系统时间,而不是玩家的时间同步。我知道球员确实发挥。如果我通过CACurrentMediaTime()到BEGINTIME,动画马上开始,因为它应该在不同步的。

修改

我可以看到从一开始就在其最终状态红色正方形,这意味着动画的开头到结束,因为它是系统时间同步,而不是AVPlayerItem时间。

  //扮演组成生活,以修饰符
    AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:【组成】;
    AVPlayer *玩家= [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:播放器];
    playerLayer.frame = [UIScreen mainScreen] .bounds;    如果(!playerItem){
        的NSLog(@playerItem空);
    }
    //设定时间
    playerItem.forwardPlaybackEndTime = totalDuration;
    playerItem.videoComposition = videoComposition;    CALayer的* aLayer = [CALayer的层]
    aLayer.frame = CGRectMake(100,100,60,60);
    aLayer.backgroundColor =的UIColor redColor] .CGColor;
    aLayer.opacity = 0.f;    CAKeyframeAnimation * keyframeAnimation2 = [CAKeyframeAnimation animationWithKeyPath:@不透明度];
    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当前媒体时间,CACurrentMediaTime());    [aLayer addAnimation:keyframeAnimation2
                  forKey:@不透明度];    [self.parentLayer addSublayer:aLayer];    AVSynchronizedLayer * synchronizedLayer =
    [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];
    synchronizedLayer.frame = [UIScreen mainScreen] .bounds;
    [synchronizedLayer addSublayer:self.parentLayer];
    [playerLayer addSublayer:synchronizedLayer];


解决方案

解决的办法是, AVSynchronizedLayer 不会在模拟器上工作,但在设备上正常工作。

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];

解决方案

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

这篇关于AVSynchronizedLayer不同步动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆