【发布时间】:2013-11-07 06:02:56
【问题描述】:
我正在开发一个视频应用程序,它可以一个接一个地播放多个视频。视频存储在AVPlayerItems 的数组中。 AVQueuePlayer 使用 AVPlayerItems 初始化,它会自动播放该数组中的视频。
问题在于,当它更改为播放下一个视频时,它会卡住几分之一秒,或者在从一个视频转换到另一个视频时产生抖动。我想在视频更改时使用某种动画(例如淡入和淡出)来改进这种过渡。
我的AVQueuePlayer代码:
AVQueuePlayer *mediaPlayer = [[AVQueuePlayer alloc] initWithItems:arrPlayerItems];
playerLayer=[AVPlayerLayer playerLayerWithPlayer:mediaPlayer];
playerLayer.frame=self.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.needsDisplayOnBoundsChange = NO;
[self.layer addSublayer:playerLayer];
self.layer.needsDisplayOnBoundsChange = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(itemPlayEnded:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[mediaPlayer currentItem]];
我尝试在过渡时创建一个新图层并通过降低其不透明度并增加新图层的不透明度来为旧图层设置动画(以创建所需的淡入淡出效果),但它不起作用根据需要。
自定义过渡的代码:
-(void)TransitionInVideos {
if (roundf(CMTimeGetSeconds(mediaPlayer.currentTime))==roundf(CMTimeGetSeconds(mediaPlayer.currentItem.duration))) {
[self.layer addSublayer:playerLayerTmp];
//Animation for the transition between videos
[self performSelector:@selector(FadeIn) withObject:nil afterDelay:0.3];
[self performSelector:@selector(FadeOut) withObject:nil afterDelay:0.3];
}
}
-(void)FadeIn {
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 2.0;
[playerLayer addAnimation:fadeAnim forKey:@"opacity"];
[self performSelector:@selector(HideLayer) withObject:nil afterDelay:2.0];
}
-(void)FadeOut {
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:0.0];
fadeAnim.toValue = [NSNumber numberWithFloat:1.0];
fadeAnim.duration = 1.0;
[playerLayerTmp addAnimation:fadeAnim forKey:@"opacity"];
[self performSelector:@selector(ShowLayer) withObject:nil afterDelay:1.0];
}
-(void)HideLayer {
playerLayer.opacity=0.0;
}
-(void)ShowLayer {
playerLayerTmp.opacity=1.0;
}
如何将过渡应用于AVQueuePlayer 中的视频?
【问题讨论】:
-
我想要同样的如果你找到了你的解决方案然后在这里发布它会帮助其他人。
-
Apple 提供了使用队列播放器和无缝转换的示例代码,如果有帮助的话:developer.apple.com/library/content/samplecode/avloopplayer/…
标签: ios avplayer avqueueplayer avplayerlayer