gpt4 book ai didi

ios - AVPlayer播放单声道音频stereo->mono

转载 作者:可可西里 更新时间:2023-11-01 06:10:43 26 4
gpt4 key购买 nike

在我的 iPad/iPhone 应用程序中,我正在使用 AVPlayer 播放视频。视频文件有一个立体声音轨,但我只需要以单声道播放该音轨的一个 channel 。部署目标是 iOS 6。我怎样才能做到这一点?非常感谢您的帮助。

最佳答案

我现在终于找到了这个问题的答案 - 至少对于在 iOS 6 上的部署来说是这样。您可以轻松地将 MTAudioProcessingTap 添加到现有的 AVPlayer 项目,并在播放期间将所选 channel 示例复制到另一个 channel 你的进程回调函数。这是解释基础知识的精彩教程: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;

下面是音频处理函数(实际上process是唯一需要的):

#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;
}
}
}

关于ios - AVPlayer播放单声道音频stereo->mono,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15174485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com