多数のビデオを次々と再生するビデオ アプリケーションに取り組んでいます。ビデオは の配列に格納されAVPlayerItem
ます。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
ですか?