iOS VoIP 应用程序 |音频队列 | AVSession 类别
Posted
技术标签:
【中文标题】iOS VoIP 应用程序 |音频队列 | AVSession 类别【英文标题】:iOS Voip Application | AudioQueue | AVSession Category 【发布时间】:2014-06-04 12:47:47 【问题描述】:在我的 ios 应用程序中,我使用 AudioQueue 进行音频录制和播放,基本上我在 iOS 上运行并移植了 OSX 版本。 我意识到在 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");
现在发生的事情是,Speaker AudioQueue 回调永远不会被调用,我检查了很多答案,cmets on so ,google 等...看起来是正确的,我做的方式是
为输入和输出创建音频队列:配置线性 PCM,16000 采样率 分配缓冲区 使用有效回调设置队列, 开始队列,似乎很好,我可以听到另一端的输出(即 Input AudioQueue 正在工作)但输出 AudioQueue(即 AudioQueueOutputCallback 永远不会被调用)。
我怀疑我需要设置正确的 AVSessionCatogery,我正在尝试使用所有可能的选项,但无法在扬声器中听到任何声音,
我将我的实现与在主线程上运行 AudioQueue 的 Apple 示例 Speakhere 进行比较。 即使我不启动 Input AudioQueue ( mic ),我也会有同样的行为。并且很难有 Speakhere 行为,即停止录制和播放
感谢您的关注,期待您的 cmets/帮助。就能分享代码sn -p。
【问题讨论】:
【参考方案1】:感谢查看,我意识到了问题,这是我的回调,
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));
因为我在分配时没有入队,并且我相信它必须在开始之前将几个缓冲区入队,删除返回部分解决了我的问题。
【讨论】:
以上是关于iOS VoIP 应用程序 |音频队列 | AVSession 类别的主要内容,如果未能解决你的问题,请参考以下文章
当用户将手机切换为静音时,如何防止我的 iOS VoIP 应用播放音频?