IOS aac裸数据创建音频SampleBuffer


- (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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值