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];
}
}为了了解更多上下文,我正在尝试为flutter的video_player插件https://github.com/flutter/flutter/issues/72878提供一个修复方案
发布于 2021-01-21 02:39:53
您可以通过将AVQueuePlayer子类化(yay OOP)并根据需要在其中创建和添加AVPlayerItemVideoOutput来实现这一点。我以前从未见过多个AVPlayerItemVideoOutput,但内存消耗似乎很合理,一切都正常。
@interface OutputtingQueuePlayer : AVQueuePlayer
@end
@implementation OutputtingQueuePlayer
- (void)insertItem:(AVPlayerItem *)item afterItem:(nullable AVPlayerItem *)afterItem;
{
if (item.outputs.count == 0) {
NSLog(@"Creating AVPlayerItemVideoOutput");
AVPlayerItemVideoOutput *videoOutput = [[AVPlayerItemVideoOutput alloc] initWithOutputSettings:nil]; // or whatever
[item addOutput:videoOutput];
}
[super insertItem:item afterItem:afterItem];
}
@end当前输出的访问方式如下:
AVPlayerItemVideoOutput *videoOutput = _player.currentItem.outputs.firstObject;
CVPixelBufferRef pixelBuffer = [videoOutput copyPixelBufferForItemTime:_player.currentTime itemTimeForDisplay:nil];
// do something with pixelBuffer here
CVPixelBufferRelease(pixelBuffer);配置就变成了:
_playerItem = [AVPlayerItem playerItemWithURL:videoURL];
_player = [OutputtingQueuePlayer queuePlayerWithItems:@[_playerItem]];
_playerLooper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
[self.view.layer addSublayer:_playerLayer];
[_player play];https://stackoverflow.com/questions/65802373
复制相似问题