在我的iOS应用程序中,我使用AudioQueue进行音频录制和播放,基本上我运行OSX版本并在iOS上移植它。
我意识到在iOS中我需要配置/设置AV会话,直到现在我已经完成了,
-(void)initAudioSession{
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
//set the audioSession category.
//Needs to be Record or PlayAndRecord to use audioRouteOverride:
success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];
if (!success) NSLog(@"AVAudioSession error setting category:%@",error);
//set the audioSession override
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
//activate the audio session
success = [session setActive:YES error:&error];
if (!success) NSLog(@"AVAudioSession error activating: %@",error);
else NSLog(@"audioSession active");
}
现在发生的事情是,扬声器AudioQueue回调永远不会被调用,我检查了很多答案,评论如此,谷歌等...并且看起来是正确的,我做的方式是< / p>
似乎很好,我能听到另一端的输出(即输入AudioQueue正在工作),但输出AudioQueue(即永远不会调用AudioQueueOutputCallback)。
我怀疑我需要设置正确的AVSessionCatogery,我正在尝试所有可能的选项,但没有能够在扬声器中听到任何声音,
我将我的实现与主线程上运行AudioQueue的Apple示例Speakhere进行比较 即使我没有启动输入AudioQueue(麦克风),那么我也是同样的行为。并且很难有Speakhere行为,即停止记录和播放
感谢您的关注,期待您的意见/帮助。将能够共享代码段。
答案 0 :(得分:0)
感谢您查看它,我意识到问题,这是我的回调,
void AudioStream::AQBufferCallback(void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inCompleteAQBuffer)
{
AudioStream *THIS = (AudioStream *)inUserData;
if (THIS->mIsDone) {
return;
}
if ( !THIS->IsRunning()){
NSLog(@" AudioQueue is not running");
**return;** // Error part
}
int bytes = THIS->bufferByteSize;
if ( !THIS->pSingleBuffer){
THIS->pSingleBuffer = new unsigned char[bytes];
}
unsigned char *buffer = THIS->pSingleBuffer;
if ((THIS->mNumPacketsToRead) > 0) {
/* lets read only firt packet */
memset(buffer,0x00,bytes);
float volume = THIS->volume();
if (THIS->volumeChange){
SInt16 *editBuffer = (SInt16 *)buffer;
// loop over every packet
for (int nb = 0; nb < (sizeof(buffer) / 2); nb++) {
// we check if the gain has been modified to save resoures
if (volume != 0) {
// we need more accuracy in our calculation so we calculate with doubles
double gainSample = ((double)editBuffer[nb]) / 32767.0;
/*
at this point we multiply with our gain factor
we dont make a addition to prevent generation of sound where no sound is.
no noise
0*10=0
noise if zero
0+10=10
*/
gainSample *= volume;
/**
our signal range cant be higher or lesser -1.0/1.0
we prevent that the signal got outside our range
*/
gainSample = (gainSample < -1.0) ? -1.0 : (gainSample > 1.0) ? 1.0 : gainSample;
/*
This thing here is a little helper to shape our incoming wave.
The sound gets pretty warm and better and the noise is reduced a lot.
Feel free to outcomment this line and here again.
You can see here what happens here http://silentmatt.com/javascript-function-plotter/
Copy this to the command line and hit enter: plot y=(1.5*x)-0.5*x*x*x
*/
gainSample = (1.5 * gainSample) - 0.5 * gainSample * gainSample * gainSample;
// multiply the new signal back to short
gainSample = gainSample * 32767.0;
// write calculate sample back to the buffer
editBuffer[nb] = (SInt16)gainSample;
}
}
}
else{
// NSLog(@" No change in the volume");
}
memcpy(inCompleteAQBuffer->mAudioData, buffer, 640);
inCompleteAQBuffer->mAudioDataByteSize = 640;
inCompleteAQBuffer->mPacketDescriptionCount = 320;
show_err(AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, NULL));
}
}
因为当它被分配时我没有入队,我相信它必须在它开始之前排队几个缓冲区,删除返回部分解决了我的问题。