这是我第一篇关于stackoverflow的帖子。目前我正在为ios开发一个voip应用程序。我想做这样的事情。
//in a thread
while(callIsOnGoing){
data = getDataFromNetwork()
playData()
sleep(10ms)
}
但问题是ios中的音频工作在“拉”模型中(使用回调来获取数据)。但我需要推送数据来播放它。我已经尝试过AudioQueue,但是在audioQueue中,虽然回调被调用,但是我在回调之外推送缓冲区的数据不会被播放。
同样,我已经看到了apple(http://developer.apple.com/library/ios/#samplecode/AVCaptureToAudioUnit/Introduction/Intro.html)的AVCaptureToAudioUnit示例,他们在音频单元延迟的情况下同步调用AudioUnitRender。我试过类似的RemoteI / O音频单元。但每次它返回OSStatus -50。 代码如下:
//in a separate thread
do { // 5
int data_length = [NativeLibraryHelper GetData:(playBuff)];
if(data_length == 0){
}else{
double numberOfFrameCount = data_length / player->audioStreamDesc->mBytesPerFrame;
currentSampleTime += numberOfFrameCount;
//AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp timeStamp;
memset(&timeStamp, 0, sizeof(AudioTimeStamp));
timeStamp.mSampleTime = currentSampleTime;
timeStamp.mFlags |= kAudioTimeStampSampleTimeValid;
AudioUnitRenderActionFlags flags = 0;
AudioBuffer buffer;
buffer.mNumberChannels = player->audioStreamDesc->mChannelsPerFrame;
buffer.mDataByteSize = data_length;
buffer.mData = malloc(data_length);
memcpy(buffer.mData, playBuff, data_length);
AudioBufferList audBuffList;
audBuffList.mBuffers[0] = buffer;
audBuffList.mNumberBuffers = 1;
printf("Audio REnder call back funciotn called with data size %d\n", data_length);
status = AudioUnitRender(audioUnitInstance, &flags, &timeStamp, 0, numberOfFrameCount, &audBuffList);
printf("osstatus %d\n", status);
}//end if else
CFRunLoopRunInMode ( // 6
kCFRunLoopDefaultMode, // 7
0.25, // 8
false // 9
);
//} while (aqData.mIsRunning);
[NSThread sleepForTimeInterval:.05];
}while (player->isRunning == YES);
我在音频播放部分挣扎了一个多月。请帮忙。提前谢谢。
答案 0 :(得分:1)
一个通用的解决方案是让异步网络getdata / read函数将数据推送到中间缓冲区或队列,然后从该中间缓冲区读取音频回调(或者如果中间缓冲区/队列为空则读取静音)。 / p>