2014-09-06 86 views
1

我正在從audiobus sdk 1.0遷移到2.0以支持ios8。但我正面臨功能代碼的一些問題。Audiobus:從sdk 1.0遷移到2.0

我想實現的是:我的應用程序是一個接收器,我想用ABReceiverPortReceive方法播放收到的音頻。

在老SDK存在ABInputPort回調方法時可用,但在SDK 2.0中沒有回調方法音頻那裏ABReceiverPort

以下是我的SDK的舊代碼1.0

_inputPort.audioInputBlock = ^(ABInputPort *inputPort, UInt32 lengthInFrames, AudioTimeStamp *nextTimestamp, ABPort *sourcePortOrNil) { 

    // get samples from audiobus 
    AudioBufferList* receiveBufferList = alloca(sizeof(AudioBufferList) + (_receiveBufferList->mNumberBuffers - 1) * sizeof(AudioBuffer)); 
    memcpy(receiveBufferList, _receiveBufferList, sizeof(AudioBufferList) + (_receiveBufferList->mNumberBuffers - 1) * sizeof(AudioBuffer)); 

    ABInputPortReceive(inputPort, sourcePortOrNil, receiveBufferList, &lengthInFrames, nextTimestamp, NULL); 

    // put samples into buffer 
    if (lengthInFrames > 0) 
    { 
     ABMultiStreamBufferEnqueue([ABPlugin plugin].inputBuffer, sourcePortOrNil, receiveBufferList, lengthInFrames, nextTimestamp); 
    } 
}; 

這裏我打電話ABInputPortReceive當在新的sdk中調用calback方法並且音頻可用但沒有回調方法時。

所以我在我的音頻引擎回調方法中調用了ABReceiverPortReceive方法。

- (OSStatus)render:(AudioBufferList *)buffer frames:(NSUInteger)numFrames timestamp:(const AudioTimeStamp *)timestamp flags:(AudioUnitRenderActionFlags *)ioFlags 
{ 

     // NSLog(@"%s", __PRETTY_FUNCTION__); 

    if (nil == self.source) 
    { 
     *ioFlags |= kAudioUnitRenderAction_OutputIsSilence; 
     return noErr; 
    } 

    // read previously enqueued by plugin samples from buffer into output buffer 
    UInt32 totalFrames = 0; 
    UInt32 framesFromBuffer = numFrames; 

    // get samples from input buffer 
    AudioBufferList* receiveBufferList = alloca(sizeof(AudioBufferList) + (_receiveBufferList->mNumberBuffers - 1) * sizeof(AudioBuffer)); 
    memcpy(receiveBufferList, _receiveBufferList, sizeof(AudioBufferList) + (_receiveBufferList->mNumberBuffers - 1) * sizeof(AudioBuffer)); 

    AudioTimeStamp bufferTimestamp = *timestamp; 
    ABMultiStreamBufferDequeueSingleSource([ABPlugin plugin].inputBuffer, self.source, receiveBufferList, &framesFromBuffer, &bufferTimestamp); 
    totalFrames += framesFromBuffer; 

    _framesPlayed += numFrames; 
    [ABPlugin plugin].framesPlayed = MAX([ABPlugin plugin].framesPlayed, _framesPlayed); 

    ABMultiStreamBufferMarkSourceIdle([ABPlugin plugin].inputBuffer, self.source); 

    // convert AB format to output stream format 
    OSStatus result = AudioConverterConvertComplexBuffer(_formatConverter, totalFrames, receiveBufferList, buffer); 

    AudioTimeStamp timestamp1 = *timestamp; 
    if (ABReceiverPortIsConnected([ABPlugin plugin].inputPort)) { 
     // Receive audio from Audiobus 
     ABReceiverPortReceive([ABPlugin plugin].inputPort, self.source, _receiveBufferList, numFrames, &timestamp1); 
    } 

    return result; 
} 

問題:無音頻這樣

請幫助或儘早提出好的建議收到。

謝謝。

+0

是否正確寫入: totalFrames + = framesFromBuffer;而不是: totalFrames = totalFrames + framesFromBuffer; 只是一個評論/問題。 – mcphersonjr 2014-09-10 21:28:39

+0

是的,它是好的....並沒有問題... – 2014-09-11 04:23:46

回答

2

看來你在這裏處理多流。可能你需要區分聲音的來源。我無法幫助代碼。

+0

以及我檢查它,你得到的點,因爲這比其他答案更好。 – 2014-09-16 07:41:06