我们如何使用AVSampleBufferDisplayLayer呈现CMSampleBufferRef?

时间:2019-01-31 06:13:40

标签: ios objective-c avfoundation avplayer cmsamplebufferref

我有这个委托方法

-(void)airPlayServer:(id)server sampleBufferReceived:(CMSampleBufferRef)sampleBuffer
{
}

这给了我sampleBuffer

现在,我需要知道如何使用AVSampleBufferDisplayLayer来呈现我的sampleBuffer。我知道我们必须使用-enqueueSampleBuffer-但是我是iOS的新手,所以我们该怎么做?

我不想将sampleBuffer转换为CGImage然后绘制它。

高度赞赏示例代码:)

1 个答案:

答案 0 :(得分:3)

像这样:

   CMSampleBufferRef sampleBufferRef = ...;

    // Force display as soon as possible

    CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBufferRef, YES);
    CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
    CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);

    [sampleBufferLayer enqueueSampleBuffer:sampleBufferRef];            
    [sampleBufferLayer setNeedsDisplay];