AVPlayer 问题记录

1,截图问题:用 copyPixelBufferForItemTime 截图失败
处理:

需要在player 播放前初始化添加好AVPlayerItemVideoOutput

截图代码

    CMTime itemTime = self.player.currentItem.currentTime;
    //没有提前初始化加好 AVPlayerItemVideoOutput 这里的Buffer 会为空
    CVPixelBufferRef pixelBuffer = [self.videoOutPut copyPixelBufferForItemTime:itemTime itemTimeForDisplay:nil];
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext
                             createCGImage:ciImage
                             fromRect:CGRectMake(0, 0,
                                                 CVPixelBufferGetWidth(pixelBuffer),
                                                 CVPixelBufferGetHeight(pixelBuffer))];
    
    //当前帧的画面
    UIImage *currentImage = [UIImage imageWithCGImage:videoImage];
    if (!currentImage) {
    // show msg
        return;
    }
    CGImageRelease(videoImage);
    //保存图片
    [self saveImage:currentImage];

你可能感兴趣的:(IOS)