4

在我的 iPad/iPhone 应用程序中,我正在使用 AVPlayer 播放视频。视频文件有一个立体声音频轨道,但我只需要以单声道播放该轨道的一个通道。部署目标是 iOS 6。我该如何实现呢?非常感谢你的帮助。

4

1 回答 1

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;
        }
    }
}
于 2013-04-01T11:18:26.930 回答