首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Voip应用程序\ AudioQueue \ AVSession类别

iOS Voip应用程序\ AudioQueue \ AVSession类别
EN

Stack Overflow用户
提问于 2014-06-04 12:47:47
回答 1查看 1K关注 0票数 0

在我的iOS应用程序中,我使用AudioQueue进行音频录制和播放,基本上我已经在iOS上运行并移植了OSX版本。

我意识到在iOS中我需要配置/设置AV会话,直到现在我已经完成了,

代码语言:javascript
复制
-(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的回调永远不会被称为,我检查了许多答案,评论在so,谷歌等.看起来是正确的,我所做的是

  • 为输入和输出创建AudioQueue :配置线性PCM,16000采样率
  • 分配缓冲器
  • 设置具有有效回调的队列,
  • 开始排队,

这似乎很好,我可以听到另一端的输出(即输入AudioQueue正在工作),但是输出AudioQueue (即AudioQueueOutputCallback永远不会被调用)。

我怀疑我需要设置适当的AVSessionCatogery,我正在尝试所有可能的选择,但在演讲者中没有听到任何声音,

我将我的实现与在主线程上运行斯派克的苹果示例AudioQueue进行比较。

即使我不开始输入AudioQueue (麦克风),我也一样的行为。而且很难有说话的行为,即停止记录和演奏。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-06 15:09:18

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

代码语言:javascript
复制
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));
    }
}

由于在分配它时我没有排队,而且我认为它必须在启动之前对几个缓冲区进行排队,因此删除返回部分解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24037899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档