【发布时间】:2014-12-02 13:11:16
【问题描述】:
简单的问题。如何使用 AVAudioEngine 播放多声道音频文件(>2 声道),以便我可以听到默认 2 声道输出(耳机/扬声器)上的所有声道。以下代码(去除了错误检查以进行呈现)播放文件的前两个通道,但我只能在插入耳机时听到它。
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
更新 好的,所以只在耳机中听到音频可能是因为我忘记在我的测试应用程序中将音频会话设置为活动状态。但我仍然只听到前两个频道...
【问题讨论】:
标签: ios objective-c audio ios8 avfoundation