AVPlayerLooper
接受一个模板AVPlayerItem
和一个AVQueuePlayer
作为设置参数,然后它在内部操作队列的项目,并且播放器不断地改变它的currentItem
.
这与 完美配合AVPlayerLayer
,它接受这个循环播放器作为参数并只渲染它,但是我如何使用它AVPlayerItemVideoOutput
,它被附加到AVPlayerItem
播放器内部有多个播放器?我如何在AVPlayerLayer
内部重现相同的事情?
AVPlayerLooper
来自文档的设置示例
NSString *videoFile = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"mov"];
NSURL *videoURL = [NSURL fileURLWithPath:videoFile];
_playerItem = [AVPlayerItem playerItemWithURL:videoURL];
_player = [AVQueuePlayer queuePlayerWithItems:@[_playerItem]];
_playerLooper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:_playerLayer];
[_player play];
这是AVPlayerItemVideoOutput
应该如何使用
[item addOutput:_videoOutput];
我想出的唯一解决方法是观察currentItem
每次从旧项目中分离视频输出并将其附加到新项目的变化,如下例所示,但这显然抵消了我试图实现的无缝播放.
- (void)observeValueForKeyPath:(NSString*)path
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context {
if (context == currentItemContext) {
AVPlayerItem* newItem = [change objectForKey:NSKeyValueChangeNewKey];
AVPlayerItem* oldItem = [change objectForKey:NSKeyValueChangeOldKey];
if(oldItem.status == AVPlayerItemStatusReadyToPlay) {
[newItem removeOutput:_videoOutput];
}
if(newItem.status == AVPlayerItemStatusReadyToPlay) {
[newItem addOutput:_videoOutput];
}
[self removeItemObservers:oldItem];
[self addItemObservers:newItem];
}
}
有关更多上下文,我正在尝试解决颤振的 video_player 插件https://github.com/flutter/flutter/issues/72878