简体   繁体   English

如何将CMSampleBufferRef转换为NSData

[英]How to convert CMSampleBufferRef to NSData

How do you convert CMSampleBufferRef to NSData? 如何将CMSampleBufferRef转换为NSData?

I've managed to get the data for an MPMediaItem by following Erik Aigner's answer on this thread , however the data is of type CMSampleBufferRef . 我已经设法通过跟踪Erik Aigner在此线程上的答案来获取MPMediaItem的数据,但数据的类型为CMSampleBufferRef

I know CMSampleBufferRef is a struct and is defined in the CMSampleBuffer Reference in the iOS Dev Library, but I don't think I fully understand what it is. 我知道CMSampleBufferRef是一个结构,在iOS Dev Library的CMSampleBuffer Reference中定义,但我不认为我完全理解它是什么。 None of the CMSampleBuffer functions seem to be an obvious solution. CMSampleBuffer函数似乎都不是一个明显的解决方案。

Here you go this works for audio sample buffer which is what you are looking at, and if you want to look at the whole process (getting all audio data from MPMediaItem into a file check out this question 在这里,您可以使用音频样本缓冲区,这是您正在查看的内容,如果您想查看整个过程(将所有音频数据从MPMediaItem获取到文件中,请查看问题

CMSampleBufferRef ref=[output copyNextSampleBuffer];
        // NSLog(@"%@",ref);
        if(ref==NULL)
            break;
        //copy data to file
        //read next one
        AudioBufferList audioBufferList;
        NSMutableData *data=[[NSMutableData alloc] init];
        CMBlockBufferRef blockBuffer;
        CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
        // NSLog(@"%@",blockBuffer);



        for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
        {
            AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
            Float32 *frame = (Float32*)audioBuffer.mData;


            [data appendBytes:frame length:audioBuffer.mDataByteSize];



        }


        CFRelease(blockBuffer);
        CFRelease(ref);
        ref=NULL;
        blockBuffer=NULL;
        [data release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM