在播放期间更改 AVPlayerItem 的 videoComposition 属性 (AVMutableVideoComposition)
Posted
技术标签:
【中文标题】在播放期间更改 AVPlayerItem 的 videoComposition 属性 (AVMutableVideoComposition)【英文标题】:Changing AVPlayerItem's videoComposition property (AVMutableVideoComposition) during playback 【发布时间】:2012-05-19 08:46:35 【问题描述】:我正在尝试在播放期间更改视频合成的布局(即其组件帧的变换)。似乎有时这是可行的,并且视频合成无缝地更改为新的转换集,但其他时候它只是冻结并保持当前的转换。 AVPlayer 实例的状态码没有变化,播放器和播放器项目都没有错误。
以前有人经历过吗?对于为什么会发生这种情况或如何解决它的任何建议,我们将不胜感激。
一些代码如下所示。重要的一点是“playerItem.videoComposition = videoComposition”,这里是在点击视频时触发的(用于测试目的)。
解决此问题的另一种方法是将视频显示在不同的图层上,但视频必须保持同步,因此合成似乎是实现此目的的唯一方法。
@implementation VideoView
CGSize _videoSize;
CMTimeRange _videoFullRange;
AVMutableCompositionTrack * _compositionTrackVideoA;
AVMutableCompositionTrack * _compositionTrackVideoB;
+ (Class)layerClass
return [AVPlayerLayer class];
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if ( self )
NSString * videoAPath = [[NSBundle mainBundle] pathForResource:@"cam09v2" ofType:@"mp4"];
NSString * videoBPath = [[NSBundle mainBundle] pathForResource:@"cam10v2_b" ofType:@"mp4"];
AVURLAsset * videoAAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoAPath] options:nil];
AVURLAsset * videoBAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoBPath] options:nil];
AVAssetTrack * videoATrack = [[videoAAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
AVAssetTrack * videoBTrack = [[videoBAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
AVAssetTrack * audioTrack = [[videoAAsset tracksWithMediaType:AVMediaTypeAudio] lastObject];
_videoSize = [videoATrack naturalSize];
CMTime videoDuration = videoAAsset.duration;
_videoFullRange = CMTimeRangeMake(kCMTimeZero, videoDuration);
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack * compositionTrackVideoA = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack * compositionTrackVideoB = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack * compositionTrackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
compositionTrackVideoA.preferredTransform = videoATrack.preferredTransform;
NSError * error = nil;
if ( ! [compositionTrackVideoA insertTimeRange:_videoFullRange ofTrack:videoATrack atTime:kCMTimeZero error:&error] )
NSLog(@"%@", error);
if ( ! [compositionTrackVideoB insertTimeRange:_videoFullRange ofTrack:videoBTrack atTime:kCMTimeZero error:&error] )
NSLog(@"%@", error);
if ( ! [compositionTrackAudio insertTimeRange:_videoFullRange ofTrack:audioTrack atTime:kCMTimeZero error:&error] )
NSLog(@"%@", error);
_compositionTrackVideoA = [compositionTrackVideoA copy];
_compositionTrackVideoB = [compositionTrackVideoB copy];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:composition];
AVPlayer * player = [AVPlayer playerWithPlayerItem:playerItem];
[(AVPlayerLayer *)self.layer setPlayer:player];
[player play];
[player addObserver:self forKeyPath:@"status" options:0 context:0];
[self updateCompositionForPlayerItem:playerItem];
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[self addGestureRecognizer:tapGesture];
return self;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
if ( [keyPath isEqualToString:@"status"] )
NSLog(@"STATUS %d", ((AVPlayer *)object).status );
- (void)updateCompositionForPlayerItem:(AVPlayerItem *)playerItem
AVMutableVideoComposition * videoComposition = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
videoInstruction.enablePostProcessing = NO;
videoInstruction.timeRange = _videoFullRange;
AVMutableVideoCompositionLayerInstruction * layerInstructionA = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:_compositionTrackVideoA];
CGAffineTransform transformA = CGAffineTransformMakeScale(0.5, 0.5);
[layerInstructionA setTransform:transformA atTime:kCMTimeZero];
AVMutableVideoCompositionLayerInstruction * layerInstructionB = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:_compositionTrackVideoB];
CGAffineTransform transformB = CGAffineTransformMakeScale(0.5, 0.5);
static int i = 0;
transformB = CGAffineTransformTranslate(transformB, (i++ % 2 == 0) ? _videoSize.width : 0, _videoSize.height);
[layerInstructionB setTransform:transformB atTime:kCMTimeZero];
videoInstruction.layerInstructions = [NSArray arrayWithObjects:layerInstructionA, layerInstructionB, nil];
videoComposition.instructions = [NSArray arrayWithObject:videoInstruction];
videoComposition.frameDuration = CMTimeMake(1, 30); // 30 fps
videoComposition.renderSize = _videoSize;
playerItem.videoComposition = videoComposition;
- (void)didTap:(UITapGestureRecognizer *)tapGesture
[self updateCompositionForPlayerItem:((AVPlayerLayer *)self.layer).player.currentItem];
@end
【问题讨论】:
这个问题你解决了吗? 【参考方案1】:您可以节省要更改它的时间,并用新的视频合成替换播放器项目,然后从您停止播放的时间开始用新的播放器项目重新启动播放器。
【讨论】:
这是一个明显的解决方法,但它有一个视觉上的“打嗝”。它只是无法正常进行。以上是关于在播放期间更改 AVPlayerItem 的 videoComposition 属性 (AVMutableVideoComposition)的主要内容,如果未能解决你的问题,请参考以下文章
AVPlayerItem 不播放视频,即使其状态为 `readyToPlay` 并且 `isPlaybackLikelyToKeepUp` 为 true
以秒为单位播放 AVPlayerItem CurrentTime 返回不一致的值