当AVPlayer播放m3u8stream时,如何捕捉图像?

我使用AVPlayer播放一个m3u8文件,并且我想在这些代码中捕获一个图像:

 AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:self.player.currentItem.asset]; gen.appliesPreferredTrackTransform = YES; NSError *error = nil; CMTime actualTime; CMTime now = self.player.currentTime; [gen setRequestedTimeToleranceAfter:kCMTimeZero]; [gen setRequestedTimeToleranceBefore:kCMTimeZero]; CGImageRef image = [gen copyCGImageAtTime:now actualTime:&actualTime error:&error]; UIImage *thumb = [[UIImage alloc] initWithCGImage:image]; NSLog(@"%f , %f",CMTimeGetSeconds(now),CMTimeGetSeconds(actualTime)); NSLog(@"%@",error); if (image) { CFRelease(image); } 

但它不起作用。 而错误是:

 Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x7fadf25f59f0 {NSUnderlyingError=0x7fadf25f1670 "The operation couldn't be completed. (OSStatus error -12782.)", NSLocalizedFailureReason=An unknown error occurred (-12782), NSLocalizedDescription=The operation could not be completed} 

我该如何解决?
非常感谢。

AVAssetImageGenerator可能需要本地资产。 也许你会有更多的运气添加一个AVPlayerItemVideoOutput到你的AVPlayer ,寻找所需的位置,并调用copyPixelBufferForItemTime:itemTimeForDisplay:在VideoOutput上的copyPixelBufferForItemTime:itemTimeForDisplay: :。

我使用下面的代码解决了同样的问题。

你可以使用这个代码:

属性

 @property (strong, nonatomic) AVPlayer *player; @property (strong, nonatomic) AVPlayerItem *playerItem; @property (strong, nonatomic) AVPlayerItemVideoOutput *videoOutput; 

初始

 AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; self.playerItem = [AVPlayerItem playerItemWithAsset:asset]; self.player = [AVPlayer playerWithPlayerItem:_playerItem]; 

获取图像

 CMTime currentTime = _player.currentItem.currentTime; CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil]; CIImage *ciImage = [CIImage imageWithCVPixelBuffer:buffer]; UIImage *image = [UIImage imageWithCIImage:ciImage]; //Use image^^