iOS Voip应用| AudioQueue | AVSession类别



在我的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回调从未被调用,我检查了许多答案,评论,谷歌等…看起来是正确的,我做的方法是

  • 为输入和输出创建AudioQueue:配置线性PCM, 16000采样率
  • 分配缓冲区
  • 设置有效回调队列,
  • 启动队列,

似乎很好,我可以听到另一端的输出(即输入AudioQueue正在工作),但输出AudioQueue(即AudioQueueOutputCallback从未被调用)。

我怀疑我需要设置适当的avsessioncategory,我正在尝试所有可能的选项,但无法听到扬声器中的任何声音,

我比较我的实现与苹果的例子Speakhere在主线程上运行AudioQueue。
即使我不启动输入AudioQueue(麦克风),然后我也同样的行为。并且很难有Speakhere行为,即停止录制并播放

谢谢你看它,期待你的评论/帮助。将能够共享代码片段。

谢谢你看它,我意识到问题,这是我的回调,

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));
    }
}

因为我在分配时没有排队,我相信它在开始之前必须排队几个缓冲区,删除返回部分解决了我的问题。

相关内容

  • 没有找到相关文章

最新更新