2015-04-02 46 views
3

我用AVPlayer發揮m3u8文件,我想在這些代碼來捕捉圖像: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} 

我該如何解決它?
非常感謝。

回答

3

AVAssetImageGenerator可能需要本地資產。也許你會有更多的運氣在你的AVPlayer上添加一個AVPlayerItemVideoOutput,尋找所需的位置並在videoOutput上調用copyPixelBufferForItemTime:itemTimeForDisplay:

1

我用下面的代碼解決了同樣的問題。

您可以使用此代碼:

屬性

@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^^ 
+0

你怎麼初始化videoOutput? – AntonijoDev 2017-08-21 10:00:24

相關問題