AudioQueue

//========================= 播放 ==============================

// 所属类 AudioPlayer
AudioQueueRef mQueue;
AudioStreamBasicDescription mDataFormat;

//创建AudioQueue
AudioQueueNewOutput(&mDataFormat, AQBufferCallback, (__bridge void *)self,  NULL, NULL, 0, &mQueue);

//生成若干 mBuffers (AudioQueueBufferRef)
for (int i = 0; i < 3; ++i) 
{
    AudioQueueAllocateBufferWithPacketDescriptions(mQueue, bufferByteSize, 0, &mBuffers[i]);
}


//回调
AudioQueueNewOutput(&mDataFormat, AQBufferCallback, (__bridge void *)self,  NULL, NULL, 0, &mQueue);

void AQBufferCallback(void *                    inUserData,
                                AudioQueueRef            inAQ,
                                AudioQueueBufferRef        inBuffer)
{
    //d 是要播放的数据(磁盘读出来的,或者其他地方来的,反正是要播放数据来源)
    memcpy(inBuffer->mAudioData,[d bytes], inBuffer->mAudioDataByteSize);//取缓存器中的音频数据
    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);//缓冲器重新放入缓存队列中
}



//========================= 录制 ==============================

// 所属类 AQRecorder
AudioQueueRef mQueue;
AudioStreamBasicDescription	mRecordFormat;

//创建AudioQueue
AudioQueueNewInput(&mRecordFormat, MyInputBufferHandler, (__bridge void *)self,NULL, NULL, 0 , &mQueue);

//回调
static void MyInputBufferHandler(	void *inUserData,//录音回调函数
										AudioQueueRef inAQ,
										AudioQueueBufferRef	inBuffer,// newly filled by the recording audio queue, containing the new audio data
										const AudioTimeStamp *inStartTime,
										UInt32 inNumPackets,
										const AudioStreamPacketDescription* inPacketDesc)

{
	AQRecorder *aqr = (__bridge AQRecorder *)inUserData;

    //将缓冲器中的数据写入磁盘
    [aqr.delegate captureData:(const char *)inBuffer->mAudioData size:inBuffer->mAudioDataByteSize];
    
    //重复利用缓冲器,将缓冲器加入缓存队列
    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}


 

 

参考:https://blog.csdn.net/midfar/article/details/7233454

你可能感兴趣的:(ios)