我正在编写一个从互联网下载音频文件的iPhone应用程序,可以在下载时播放它们(如果网络缓冲区为空则播放白噪声),所以,我认为,我不能只使用AVAudioPlayer而应填写音频缓冲我自己。文件是MP3格式的,应该是硬件解码的。我的第一次尝试是AudioUnit:
AudioStreamBasicDescription audioFormat;
bzero(&audioFormat, sizeof(AudioStreamBasicDescription));
audioFormat.mSampleRate = 44100;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mFormatID = kAudioFormatMPEGLayer3;
audioFormat.mFramesPerPacket = 1152;
然后用MP3数据包填充缓冲区,但这不起作用,因为远程IO不能用于压缩流,所以我决定深入音频队列:
static void HandleOutputBuffer (
void *aqData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer
) {
AQPlayerState *pAqData = (AQPlayerState *) aqData; // 1
if (pAqData->mIsRunning == 0) return; // 2
UInt32 numBytesReadFromFile; // 3
UInt32 numPackets = pAqData->mNumPacketsToRead; // 4
AudioFileReadPackets (
pAqData->mAudioFile,
false,
&numBytesReadFromFile,
pAqData->mPacketDescs,
pAqData->mCurrentPacket,
&numPackets,
inBuffer->mAudioData
);
if (numPackets > 0) { // 5
inBuffer->mAudioDataByteSize = numBytesReadFromFile; // 6
AudioQueueEnqueueBuffer (
pAqData->mQueue,
inBuffer,
(pAqData->mPacketDescs ? numPackets : 0),
pAqData->mPacketDescs
);
pAqData->mCurrentPacket += numPackets; // 7
} else {
AudioQueueStop (
pAqData->mQueue,
false
);
pAqData->mIsRunning = false;
}
}
问题是:pAqData->mAudioFile
会以这种方式进行硬件解码吗?另一个问题是:如何将存储在NSData中的mp3数据提供给AudioQueueEnqueueBuffer?