2013-03-02 160 views

回答

1

我現在終於找到了這個問題的答案 - 至少在iOS 6的部署您可以將過程的回調函數過程中容易添加MTAudioProcessingTap到您現有的AVPlayer項目,並複製所選通道的樣本給另一個信道。下面是一個很好的教程,解釋基本知識:http://chritto.wordpress.com/2013/01/07/processing-avplayers-audio-with-mtaudioprocessingtap/

這是我迄今爲止的代碼,主要是從上面的鏈接複製的。

在AVPlayer設置我分配回調函數用於音頻處理:

MTAudioProcessingTapCallbacks callbacks; 
callbacks.version = kMTAudioProcessingTapCallbacksVersion_0; 
callbacks.clientInfo = (void *)(self); 
callbacks.init = init; 
callbacks.prepare = prepare; 
callbacks.process = process; 
callbacks.unprepare = unprepare; 
callbacks.finalize = finalize; 

MTAudioProcessingTapRef tap; 
// The create function makes a copy of our callbacks struct 
OSStatus err = MTAudioProcessingTapCreate(kCFAllocatorDefault, &callbacks, 
              kMTAudioProcessingTapCreationFlag_PostEffects, &tap); 
if (err || !tap) { 
    NSLog(@"Unable to create the Audio Processing Tap"); 
    return; 
} 
assert(tap); 

// Assign the tap to the input parameters 
audioInputParam.audioTapProcessor = tap; 

// Create a new AVAudioMix and assign it to our AVPlayerItem 
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
audioMix.inputParameters = @[audioInputParam]; 
playerItem.audioMix = audioMix; 

下面是音頻處理功能(實際上過程是需要的只有一個):

#pragma mark Audio Processing 

void init(MTAudioProcessingTapRef tap, void *clientInfo, void **tapStorageOut) { 
    NSLog(@"Initialising the Audio Tap Processor"); 
    *tapStorageOut = clientInfo; 
} 

void finalize(MTAudioProcessingTapRef tap) { 
    NSLog(@"Finalizing the Audio Tap Processor"); 
} 

void prepare(MTAudioProcessingTapRef tap, CMItemCount maxFrames, const AudioStreamBasicDescription *processingFormat) { 
    NSLog(@"Preparing the Audio Tap Processor"); 
} 

void unprepare(MTAudioProcessingTapRef tap) { 
    NSLog(@"Unpreparing the Audio Tap Processor"); 
} 

void process(MTAudioProcessingTapRef tap, CMItemCount numberFrames, 
     MTAudioProcessingTapFlags flags, AudioBufferList *bufferListInOut, 
     CMItemCount *numberFramesOut, MTAudioProcessingTapFlags *flagsOut) { 
    OSStatus err = MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut, flagsOut, NULL, numberFramesOut); 
    if (err) NSLog(@"Error from GetSourceAudio: %ld", err); 

    SIVSViewController* self = (SIVSViewController*) MTAudioProcessingTapGetStorage(tap); 

    if (self.selectedChannel) { 

     int channel = self.selectedChannel; 

     if (channel == 0) { 
      bufferListInOut->mBuffers[1].mData = bufferListInOut->mBuffers[0].mData; 
     } else { 
      bufferListInOut->mBuffers[0].mData = bufferListInOut->mBuffers[1].mData; 
     } 
    } 
} 
+0

嗨,尋找一個解決方案在一個耳機通道上播放TTS聲音。您的代碼可能會有幫助,或者您有任何提示? – 2013-09-12 16:44:46

+0

嗨,本,我只是編輯了我的答案,並添加了我的源代碼。希望它能幫助你。 – 2014-10-30 15:09:15

+0

有誰知道你會怎樣在Swift中寫這個。似乎無法得到它的工作? – villy393 2016-02-18 14:13:52