简单的问题。如何使用AVAudioEngine播放多声道音频文件(> 2声道),以便我可以在默认双声道输出(耳机/扬声器)上听到所有声道。下面的代码(剥离错误检查呈现)播放该文件前两个渠道,但我只能听到它,当插入耳机。AVAudioEngine播放多声道音频
AVAudioFile *file = [[AVAudioFile alloc] initForReading:[[NSBundle mainBundle] URLForResource:@"nums6ch" withExtension:@"wav"] error:nil];
AVAudioEngine *engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
AVAudioMixerNode *mixer = [[AVAudioMixerNode alloc] init];
[engine attachNode:player];
[engine attachNode:mixer];
AVAudioFormat *processingFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:file.processingFormat.streamDescription->mSampleRate channels:2 interleaved:false];
[engine connect:player to:mixer format:processingFormat];
[engine connect:mixer to:engine.outputNode format:processingFormat];
[engine startAndReturnError:nil];
[player scheduleFile:file atTime:nil completionHandler:nil];
[player play];
我尝试了很多组合与格式,从播放器 - >混频器和混频器 - >输出连接,但它们要么具有同样的事情,上面的代码或者更可能崩溃与两种效果:
ERROR: [0x19c392310] AVAudioNode.mm:495: AUGetFormat: required condition is false: nil != channelLayout && channelLayout.channelCount == asbd.mChannelsPerFrame
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: nil != channelLayout && channelLayout.channelCount == asbd.mChannelsPerFrame'
或AUSetFormat OSStatus代码-10868(如果我没有记错的话这是不支持的格式)。对于任何连接使用file.processingFormat会使上面的第一个错误导致应用程序崩溃。此外,崩溃发生在[player play];
必须有某种方式播放它像我想要的,因为我能够做到这一点没有问题使用AUGraph,但是,因为AVAudioEngine提供了一个功能,我必须包括,我坚持下去。我用它来检查我的代码
多通道音频文件可以发现here
UPDATE 好了,听音频耳机只可能是因为我忘了在我的测试应用程序设置音频会话活跃。但我仍然只听到前两个频道...
请详细说明您提到的错误。是不是可以使用'AVAudioPlayerNode'的'scheduleBuffer:atTime:options:completionHandler'来为所有三个节点指定相同的'AVAudioTIme'? – Mark 2016-01-29 18:16:18
在编写本问答时,即使您为所有三个节点放置相同的AVAudioTime,声音之间仍会有轻微的延迟。不知道他们是否修好了。 – Kegluneq 2016-02-01 12:23:23