AudioStreamBasicDescription

转自 : http://blog.sina.com.cn/s/blog_6568e7880100uf6v.html

AudioStreamBasicDescription format; // 声音格式设置,这些设置要和采集时的配置一致

memset(&format, 0, sizeof(format));

 

format.mSampleRate = 44100; // 采样率 (立体声 = 8000)

format.mFormatID = kAudioFormatLinearPCM; // PCM 格式

format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

format.mChannelsPerFrame = 1;  // 1:单声道;2:立体声

format.mBitsPerChannel = 16; // 语音每采样点占用位数

format.mBytesPerFrame = (format.mBitsPerChannel/8)  * format.mChannelsPerFrame;//每桢的bytes数

format.mFramesPerPacket = 1; //每个数据包下的桢数,即每个数据包里面有多少桢

format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket; //每个数据包的bytes总数,每桢的bytes数*每个数据包的桢数

 

AudioQueueRef queue;

AudioQueueNewOutput(&format,

                AQPlayer::AQOutputCallback,

                this,  // opaque reference to whatever you like

                CFRunLoopGetCurrent(),

                kCFRunLoopCommonModes,

                0,

                &queue);

 

const int bufferSize = 0xA000;  // 48K - around 1/2 sec of 44kHz 16 bit mono PCM

for (int i = 0; i < kNumberBuffers; ++i)

    AudioQueueAllocateBufferWithPacketDescriptions(queue, bufferSize, 0, &mBuffers[i]);

 

AudioQueueSetParameter(queue, kAudioQueueParam_Volume, 1.0);

 

UInt32 category = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);

 

AudioSessionSetActive(true);

 

// prime the queue with some data before starting

for (int i = 0; i < kNumberBuffers; ++i)

    OutputCallback(queue, mBuffers[i]);

 

AudioQueueStart(queue, NULL);

 

在CallBack中填出数据:

 

void OutputCallback(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inCompleteAQBuffer) {

    // Fill

    //AQPlayer* that = (AQPlayer*) inUserData;

    inCompleteAQBuffer->mAudioDataByteSize = next->mAudioDataBytesCapacity;

    for (int i = 0; i < inCompleteAQBuffer->mAudioDataByteSize; ++i)

        next->mAudioData[i] = rand();

    AudioQueueEnqueueBuffer(queue, inCompleteAQBuffer, 0, NULL);

 }


你可能感兴趣的:(null,callback,reference)