AVAudioEngine crashes when plug headphones in or out
Asked Answered
U

1

5

I'm testing my audioApp on my iPhone6 Plus, and it crashes after plug headphone in or out. AVAudioEngine, AVAudioMixer, AVAudioPlayerNode is used in my app.

starting app without headphone can play sound from iPhone speaker.

starting app with headphone can play sound from headphone.

The error occurs in following condition. starting app without headphone and play sound from speaker. Then if I connect headphone there is no sound coming from headphone. when I unplug my headphone after that, there is still no sound coming even from speaker. and audioApp crashes when I touch the screen.

the same as starting app with headphone and plug my headphone out.

How can I fix this error? I'd appreciate your advice.

here is the error message I got.

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()'
*** First throw call stack:
(0x184d61e48 0x1954a80e4 0x184d61d08 0x1836d26c0 0x1836fa328 0x1836f975c 0x1000d03c0 0x1000d0728 0x1895450f8 0x18952e22c 0x189544a94 0x189544720 0x18953dc74 0x18951138c 0x1897b01b4 0x18950f8f4 0x184d1a0e8 0x184d1938c 0x184d1743c 0x184c451f4 0x18ddd35a4 0x189576784 0x1000d5614 0x1000d5654 0x195b16a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Usherette answered 4/11, 2014 at 5:22 Comment(0)
H
5

You need to register to the AVAudioEngineConfigurationChangeNotification notification and do the necessary steps to update the output node.

Hardcore answered 14/11, 2014 at 23:53 Comment(6)
worked, thanks! I also use this link how to change label text while on/off headphone.Usherette
So what are the necessary steps for updating the output node? I'm using mainMixer node, don't use engine's output node at all. But when I plug in the headphones, the engine doesn't get started.Isauraisbel
If you just plugged a new output node, you need to connect it to your mainMixer node.Hardcore
@Hardcore Can you please post some sample code here? I don't have any other output node except for main mixer singleton, so I don't really understand what reconnections should be done. I saw the Apple's example here and made my reconnections of AVAudioPlayerNode instances, but it doesn't help at all.Isauraisbel
@noir_eagle The situation is varied by your codes. The concept is to remove everything and connect them according to the new changes. Therefore, the procedure could be similar like this: for (node in nodes ) { [engine disconnectNodeOutput: node]; AVAudioMixerNode *mainMixer = [engine mainMixerNode]; [engine connect:node to: mainMixer format: format]; }Hardcore
@Hardcore can you please share an full functional swift example. your small example doesn't help me. thxCatastrophism

© 2022 - 2024 — McMap. All rights reserved.