我正在编写一个iPhone应用程序,我正在使用音频队列服务播放一些单声道mp3文件。在播放时,我只能在一个频道上听到声音。我一直在寻找一个如何在两个频道上播放文件的例子,没有运气。我现在正在做的事情非常简单。我正在设置我的音频队列:
AudioStreamBasicDescription queueASBD;
AudioQueueRef audioQueue;
queueASBD.mSampleRate = 44100.0;
queueASBD.mFormatID = kAudioFormatLinearPCM;
queueASBD.mFormatFlags = kAudioFormatFlagsNativeEndian | AudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
queueASBD.mBytesPerPacket = 4;
queueASBD.mFramesPerPacket = 1;
queueASBD.mBytesPerFrame = 4;
queueASBD.mChannelsPerFrame = 2;
queueASBD.mBitsPerChannel = 16;
queueASBD.mReserved = 0;
AudioQueueNewOutput(&queueASBD, AudioQueueCallback, NULL, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &audioQueue);
我打开这样的mp3文件(错误检查,为简洁而删除):
ExtAudioFileRef audioFile;
ExtAudioFileOpenURL(url, &audioFile);
ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(queueASBD), &queueASBD);
为了排队缓冲区,我做了类似的事情:
AudioQueueBufferRef buffers; // previously allocated
AudioBufferList abl;
UInt32 length = (UInt32)queueASBD.mSampleRate / BUFFERS_PER_SECOND;
abl.mNumberBuffers = 1;
abl.mBuffers[0].mDataByteSize = (UInt32)(queueASBD.mSampleRate * queueASBD.mBytesPerPacket / BUFFERS_PER_SECOND);
abl.mBuffers[0].mNumberChannels = queueASBD.mChannelsPerFrame;
abl.mBuffers[0].mData = buffer->mAudioData;
ExtAudioFileRead(audioFile, &length, &abl);
UInt32 byte_length = length * (UInt32)queueASBD.mBytesPerPacket;
buffer->mAudioDataByteSize = byte_length;
AudioQueueEnqueueBuffer(audioQueue, buffer, 0, NULL);
有没有办法让文件在没有完全重新编码的情况下以立体声方式播放(例如使用音频单元API)?音频转换器可以帮助吗?还有其他方法吗?谢谢你的帮助。
答案 0 :(得分:1)
尝试打开音频队列,每帧只有一个频道(例如单声道),以及每个数据包和每帧匹配的字节数(可能是2)。