- (CMSampleBufferRef)createAudioSampleBuffer:(char*) buf withLen:(int) len withASBD:(AudioStreamBasicDescription) asbd{
AudioBufferList audioData;
audioData.mNumberBuffers = 1;
char* tmp = malloc(len);
memcpy(tmp, buf, len);
audioData.mBuffers[0].mData = tmp;
audioData.mBuffers[0].mNumberChannels = adbs.;
audioData.mBuffers[0].mDataByteSize = len;
CMSampleBufferRef buff = NULL;
CMFormatDescriptionRef format =NULL;
OSStatus status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &asbd,0, NULL, 0, NULL, NULL, &format);
if (status) {
return;
}
CMSampleTimingInfo timing = {CMTimeMake(asbd.mFramesPerPacket,asbd.mSampleRate), kCMTimeZero, kCMTimeInvalid };
status = CMSampleBufferCreate(kCFAllocatorDefault,NULL, false,NULL, NULL, format, (CMItemCount)asbd.mFramesPerPacket,1, &timing, 0,NULL, &buff);
if (status) { //失败
return nil;
}
status = CMSampleBufferSetDataBufferFromAudioBufferList(buff,kCFAllocatorDefault,kCFAllocatorDefault,0, &audioData);
if (tmp) {
free(tmp);
}
CFRelease(format);
return buff;
}
//AudioStreamBasicDescription例子
AudioStreamBasicDescription asbd = {0};
asbd.mFormatID = kAudioFormatMPEG4AAC;
asbd.mFormatFlags = 0;
asbd.mSampleRate = 44100;
asbd.mChannelsPerFrame = 2;
asbd.mBitsPerChannel = 16;
asbd.mFramesPerPacket = 1024;
asbd.mBytesPerFrame = asbd.mBitsPerChannel * asbd.mChannelsPerFrame / 8;
asbd.mBytesPerPacket = asbd.mFramesPerPacket*asbd.mBytesPerFrame;