Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

ios - AVAudioEngine playing multi channel audio

Simple question. How do I play multi channel audio files (>2 channels) using AVAudioEngine so that I can hear all channels on default 2-channel output (headphones/speaker). Following code (stripped of error checking for presenting) plays the file first two channels but I can only hear it when headphones are plugged in.

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];

I tried a lot of combinations with formats for both player->mixer and mixer->output connections but they either result with same thing as code above or more likely a crash with either:

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'

or AUSetFormat OSStatus code -10868 (which is format not supported if I'm not mistaken). Using file.processingFormat for any connection crashes app with the first error above. Also, the crash occurs on [player play];

There must be some way of playing it like I want because I'm able to do it without problem using AUGraph, however, since AVAudioEngine provides one feature I have to include, I'm stuck with it.

Multi channel audio files I use to check my code can be found here

UPDATE Ok, so hearing audio in headphones only was probably due to me forgetting setting audio session to active in my test app. But I still only hear first two channels...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a similar issue in Swift. The error was 'com.apple.coreaudio.avfaudio', reason: 'required condition is false:!nodeimpl->SslEngineImpl()'.

The task was to play two audio files, one after another. If I hit stop after playing the first audio file and then played the second audio file, the system crashed.

I found that in a function I created I had audioEngine.attachNode(audioPlayerNode) which means that the audioPlayerNode was being attached to the audioEngine once and then exited. So I moved this attachment to the viewDidLoad() function so that it gets passed every time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...