[英]How to add transition between videos when using AVQueuePlayer?
I am working on a video application that plays a number of videos one after another. 我正在制作一个视频应用程序,一个接一个地播放大量视频。 The videos are stored in an array of
AVPlayerItem
s. 视频存储在
AVPlayerItem
数组中。 AVQueuePlayer
is initialized with those AVPlayerItems
and it automatically plays the videos from that array. AVQueuePlayer
使用这些AVPlayerItems
初始化,并自动播放该阵列中的视频。
The issue is when it changes to play the next video it gets stuck for a fraction of a second or it creates a jerk at the time it transitions from one to another. 问题是当它改变播放下一个视频时它会卡住一秒钟,或者它在从一个视频转换到另一个视频时产生一个混蛋。 I want to improve this transition with some kind of animation such as Fade In and Fade Out while the video is changed.
我希望通过某种动画来改善这种转换,例如在视频改变时淡入和淡出。
My code for AVQueuePlayer
: 我的
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]];
I tried to create a new layer at the time of the transition and animate the old layer by decreasing its opacity and increasing the opacity of the new layer (to create the desired fade in fade out effect), but it is not working as desired. 我尝试在转换时创建一个新图层,并通过降低其不透明度并增加新图层的不透明度来设置旧图层的动画(以在淡出效果中创建所需的淡入淡出),但它无法按预期工作。
The code for the custom transition: 自定义转换的代码:
-(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;
}
How can a transition be applied to videos in the AVQueuePlayer
? 如何将过渡应用于
AVQueuePlayer
中的AVQueuePlayer
?
We can set the opacity for AVMutableVideoCompositionLayerInstruction
that will be add to the video while exporting or AVPlayer
as VideoComposition property and that will create the fade in and fade out effect. 我们可以设置
AVMutableVideoCompositionLayerInstruction
的不透明度,它将在导出时添加到视频或AVPlayer
作为VideoComposition属性,并将创建淡入和淡出效果。
[layerInstruction setOpacityRampFromStartOpacity:1.0 toEndOpacity:0.0 timeRange:CMTimeRangeMake(CMTimeSubtract([mutableComposition duration], CMTimeMake(Transition_Time, 1)), [mutableComposition duration])];
[layerInstruction setOpacityRampFromStartOpacity:0.0 toEndOpacity:1.0 timeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(1, 1))];
this are the code for giving the effect. 这是给出效果的代码。
More complete examples: 更完整的例子:
we had exactly the same problem in one of our app - and unfortunately, we had to drop the standard player and use logic from this example: 我们在其中一个应用程序中遇到了完全相同的问题 - 不幸的是,我们不得不放弃标准播放器并使用此示例中的逻辑:
https://developer.apple.com/library/ios/samplecode/StitchedStreamPlayer/Listings/Classes_MyStreamingMovieViewController_m.html#//apple_ref/doc/uid/DTS40010092-Classes_MyStreamingMovieViewController_m-DontLinkElementID_8 https://developer.apple.com/library/ios/samplecode/StitchedStreamPlayer/Listings/Classes_MyStreamingMovieViewController_m.html#//apple_ref/doc/uid/DTS40010092-Classes_MyStreamingMovieViewController_m-DontLinkElementID_8
hope that helps. 希望有所帮助。
I was able to achieve the desired effect by using two separate AVPlayer objects playing back to back. 通过使用两个独立播放的AVPlayer对象,我能够达到预期的效果。 The players are using different views to play on.
玩家正在使用不同的视图进行游戏。
The idea is to fade in the second video player's view before the first video is going to finish. 这个想法是在第一个视频完成之前淡出第二个视频播放器的视图。
The demo project can be found here: IHLoopingVideoPlayerView 演示项目可以在这里找到: IHLoopingVideoPlayerView
It loops the same video over and over but the same idea can be applied to any number of videos. 它会反复循环播放相同的视频,但同样的想法可以应用于任意数量的视频。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.