iOS AudioUnit在回到后台模式时回调打嗝

时间:2017-06-01 07:12:48

标签: ios audiounit

我正在编写一个在iOS设备上播放RTP音频流的应用程序。它在前景和后台模式下都能很好地工作,但是当切换到背景时它会停留大约一秒钟。试图调查这个问题,我发现在转换到后台模式期间,AudioUnit回调函数调用之间的时间从5毫秒上升到100毫秒,并且当转换完成时 - 回落到5毫秒。 音频初始化代码:

-(BOOL)initOutput {
    OSStatus status;

    AudioStreamBasicDescription audioFormat;

    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    AudioComponent component = AudioComponentFindNext(NULL, &desc);

    status = AudioComponentInstanceNew(component, &audioUnit);
    if (status != 0) {
        NSLog(@"Error creating audio component instance");
        return false;
    }

    audioFormat.mSampleRate         = samplerate; // 48000
    audioFormat.mFormatID           = kAudioFormatLinearPCM;
    audioFormat.mFormatFlags        = kAudioFormatFlagsCanonical;
    audioFormat.mFramesPerPacket    = 1;
    audioFormat.mChannelsPerFrame   = channels; // 2
    audioFormat.mBitsPerChannel     = 16;
    audioFormat.mBytesPerPacket     = channels * 2;
    audioFormat.mBytesPerFrame      = audioFormat.mBytesPerPacket;
    audioFormat.mReserved           = 0;

    status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &audioFormat, sizeof(audioFormat));
    if (status != 0) {
        NSLog(@"Error setting kAudioUnitProperty_StreamFormat %i", status);
        return false;
    }

    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = playbackCallback;
    callbackStruct.inputProcRefCon = (__bridge void*)self;
    status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &callbackStruct, sizeof(callbackStruct));
    if (status != 0) {
        NSLog(@"Error setting kAudioUnitProperty_SetRenderCallback");
        return false;
    }

    status = AudioUnitInitialize(audioUnit);
    if (status != 0) {
        NSLog(@"Error initializing unit");
        return false;
    }

    return true;
}

AVAudioSession类别是AVAudioSessionCategoryPlayback。 CPU负载永远不会高于10%。我做错了什么,以及如何解决它?

0 个答案:

没有答案