如何在Swift中将CMSampleBuffer转换为数据?

我需要将CMSampleBuffer转换为Data格式。 我正在使用一个第三方框架进行音频相关任务。 该框架为我提供了CMSampleBuffer对象中的流媒体(即实时音频)音频。

喜欢这个:

 func didAudioStreaming(audioSample: CMSampleBuffer!) { //Here I need to conver this to Data format. //Because I am using GRPC framework for Audio Recognization, } 

请提供将CMSampleBuffer转换为Data的步骤。

FYI

  let formatDesc:CMFormatDescription? = CMSampleBufferGetFormatDescription(audioSample)  { mediaType:'soun' mediaSubType:'lpcm' mediaSpecific: { ASBD: { mSampleRate: 16000.000000 mFormatID: 'lpcm' mFormatFlags: 0xc mBytesPerPacket: 2 mFramesPerPacket: 1 mBytesPerFrame: 2 mChannelsPerFrame: 1 mBitsPerChannel: 16 } cookie: {(null)} ACL: {(null)} FormatList Array: {(null)} } extensions: {(null)} } 

尝试以下代码将CMSampleBuffer转换为NSData。

 let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0)) let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!) let height = CVPixelBufferGetHeight(imageBuffer!) let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!) let data = NSData(bytes: src_buff, length: bytesPerRow * height) CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0)) 

编辑-

对于AudioBuffer使用下面的代码 –

 var audioBufferList = AudioBufferList() var data = Data() var blockBuffer : CMBlockBuffer? CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, MemoryLayout.size, nil, nil, 0, &blockBuffer) let buffers = UnsafeBufferPointer(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers)) for audioBuffer in buffers { let frame = audioBuffer.mData?.assumingMemoryBound(to: UInt8.self) data.append(frame!, count: Int(audioBuffer.mDataByteSize)) }