gpt4 book ai didi

iphone - AVAssetReader 和音频队列流问题

转载 作者:行者123 更新时间:2023-12-03 20:56:36 24 4
gpt4 key购买 nike

我在使用 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
);


}

最佳答案

我也遇到了同样令人困惑的错误。果然,“设置” Audio Session 使错误消失了。这就是我设置 Audio Session 的方式。

- (void)setupAudio {
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];

NSLog(@"setupAudio ACTIVATION ERROR IS %@", activationError);
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.1 error:&activationError];
NSLog(@"setupAudio BUFFER DURATION ERROR IS %@", activationError);
}

关于iphone - AVAssetReader 和音频队列流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com