音頻佇列服務提供一個可能,那就是把音頻資料區塊填充到音頻佇列服務緩衝區中,從而達到播放聲音的目的,這種方式很類似
Windows 中的 waveOutWrite 方法。這樣,我們就可以通過這個方法實現播放從網路上傳輸過來的音頻資料。
我們需要通過佇列服務提供的 AQOutputCallback 回調中填充緩衝區,在這裡,我們就可以填充從網路傳輸過來的資料。
參看代碼:
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 / * format.mChannelsPerFrame;
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket;
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);
}