AVAssetReader 和音频队列流问题
我在使用 AVAssetReader 时遇到问题,无法从 iPod 库获取样本并通过音频队列进行流式传输。我无法找到任何这样的示例,因此我尝试实现自己的示例,但似乎 AssetReader 在音频队列的回调函数处“搞砸了”。具体来说,当它执行 copyNextSampleBuffer 时,它会失败,即,当它尚未完成时,它会返回 null。我已经确保指针存在,因此如果有人可以提供帮助,那就太好了。
下面是我使用过的回调函数代码。当 AudioQueue 回调未调用此回调函数时,该函数会“起作用”。
static void HandleOutputBuffer (
void *playerStateH,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer
) {
AQPlayerState *pplayerState = (AQPlayerState *) playerStateH;
//if (pplayerState->mIsRunning == 0) return;
UInt32 bytesToRead = pplayerState->bufferByteSize;
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_callsample object:nil];
float * inData =(float *) inBuffer->mAudioData;
int offsetSample = 0;
//Loop until finish reading from the music data
while (bytesToRead) {
/*THIS IS THE PROBLEMATIC LINE*/
CMSampleBufferRef sampBuffer = [pplayerState->assetWrapper getNextSampleBuffer]; //the assetreader getting nextsample with copyNextSampleBuffer
if (sampBuffer == nil) {
NSLog(@"No more data to read from");
// NSLog(@"aro status after null %d",[pplayerState->ar status]);
AudioQueueStop (
pplayerState->mQueue,
false
);
pplayerState->mIsRunning = NO;
return;
}
AudioBufferList audioBufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
AudioBuffer audioBuffer = audioBufferList.mBuffers[0];
memcpy(inData + (2*offsetSample),audioBuffer.mData,audioBuffer.mDataByteSize);
bytesToRead = bytesToRead - audioBuffer.mDataByteSize;
offsetSample = offsetSample + audioBuffer.mDataByteSize/8;
}
inBuffer->mAudioDataByteSize = offsetSample*8;
AudioQueueEnqueueBuffer (
pplayerState->mQueue,
inBuffer,
0,
0
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了同样的神秘错误。果然,“设置”音频会话使错误消失了。这就是我设置音频会话的方式。
I was getting this same mystifying error. Sure enough, "setting up" an audio session made the error go away. This is how I set up my audio session.
来自音频会话编程指南,在 AVAudioSessionCategoryAmbient 下:
使用 AVAssetReader 可能会使用 iOS 的硬件解码器,这会阻止 AudioQueue 的使用。设置 AVAudioSessionCategoryAmbient 意味着音频在软件中呈现,允许两者同时工作 - 但是,这会对性能/电池寿命产生影响。 (请参阅音频会话编程指南下的“类别如何影响编码和解码”)。
From the Audio Session Programming Guide, under AVAudioSessionCategoryAmbient:
Using an AVAssetReader probably uses iOS' hardware decoder, which blocks the use of the AudioQueue. Setting AVAudioSessionCategoryAmbient means the audio is rendered in software, allowing both to work at the same time - however, this would have an impact on performance/battery life. (See Audio Session Programming Guide under "How Categories Affect Encoding and Decoding").
好吧,我已经以某种方式解决了这个奇怪的错误...显然这是因为音频会话设置不正确。谈论缺乏这方面的文档......
Ok I have somehow solved this weird error... Apparently it is because of the audio session not properly set. Talk about lack of documentation on this one...