In my iPad/iPhone App I'm playing back a video using AVPlayer. The video file has a stereo audio track but I need to playback only one channel of this track in mono. Deployment target is iOS 6. How can I achieve this? Thanks a lot for your help.
Asked
Active
Viewed 2,864 times
1 Answers
1
I now finally found an answer to this question - at least for deployment on iOS 6. You can easily add an MTAudioProcessingTap to your existing AVPlayer item and copy the selected channels samples to the other channel during your process callback function. Here is a great tutorial explaining the basics: http://chritto.wordpress.com/2013/01/07/processing-avplayers-audio-with-mtaudioprocessingtap/
This is my code so far, mostly copied from the link above.
During AVPlayer setup I assign callback functions for audio processing:
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;
Here are the audio processing functions (actually process is the only one needed):
#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;
}
}
}

Andreas Zöllner
- 233
- 3
- 12
-
Hey, looking for a solution to play TTS sounds on one headphone channel. Your code could be helpful or do you have any tips? – Ben Hutchinson Sep 12 '13 at 16:44
-
Hi Ben, I just edited my answer and added my source code. Hope it helps you out. – Andreas Zöllner Oct 30 '14 at 15:09
-
Does anyone know how you would write this in Swift. Can't seem to get it to work? – villy393 Feb 18 '16 at 14:13
-
Hi, Im having problems to correclty deallocate the tap processor after the object containing it, please see my issue here: https://es.stackoverflow.com/questions/111080/correctly-deallocating-mtaudioprocessingtap-in-swift3-2-4. – glm4 Oct 20 '17 at 19:58