2016-08-03 189 views
0

我试图让播放器在视频暂停时暂停。我不知道如何去做这件事,无论我把它添加为另一个calayer或动画。当我在导出之前添加calayer时,这是我的代码。所以当动画发生时,我希望视频在出现时暂停,然后在动画停止后继续。如何在播放器中出现视频暂停

let titleLayer = CATextLayer() 
    titleLayer.backgroundColor = NSColor.clearColor().CGColor 
    titleLayer.string = "Dummy text" 
    titleLayer.font = NSFont(name: "Helvetica", size: 28) 
    titleLayer.shadowOpacity = 0.5 
    titleLayer.alignmentMode = kCAAlignmentCenter 
    titleLayer.frame = CGRectMake(0, 50, size.width, size.height/6) 

    let animation: CABasicAnimation = CABasicAnimation(keyPath: "opacity") 
    animation.duration = 0 
    animation.fromValue = Int(1.0) 
    animation.toValue = Int(0.0) 
    animation.beginTime = 5 
    animation.removedOnCompletion = false 
    animation.fillMode = kCAFillModeForwards 
    titleLayer.addAnimation(animation, forKey: "animateOpacity") 

回答

1

您可以在将动画添加到图层之前暂停视频,然后在动画完成时恢复它。要知道动画何时完成,您可以让一个对象成为animation的代表,然后在animationDidStop:finished:委托方法中恢复播放。

func showTitle() { 
    // configure animation { .. } 
    animation.delegate = self 
    // pause video 
    titleLayer.addAnimation(animation, forKey: "animateOpacity") 
} 

func animationDidStop(anim: CAAnimation, finished flag: Bool) { 
    // play video 
} 
+0

谢谢你的答案@gravicle。最后一个问题,我将如何使用avexportsession来导出这个问题,以便在quicktime移动播放时显示动画时间的暂停,然后恢复? – spe

+1

@spel你必须为此使用'AVMutableComposition'。这个问题中的代码片段几乎完成了你想要做的事情:http://stackoverflow.com/questions/10887449/black-video-caanimation-and-avfoundation-avassetexportsession – gravicle

相关问题