How to connect AVAudioEngine to the audio input of the Lightning Port?

I want to connect my electric guitar to my application. I have hardware (Line6 Sonic Port) that transfers audio from my guitar to my iPhone. I figured out how to listen to the sound in the headphones, but the sound comes from the headphone microphone, not the Lightning Port. How to programmatically find the audio input of a Lightning Port instead of receiving sound through a headphone microphone?

Here is what I have tried so far:

self.audioEngine = AVAudioEngine() let input = self.audioEngine.inputNode let mixer = self.audioEngine.mainMixerNode let output = self.audioEngine.outputNode self.audioEngine.inputNode.installTapOnBus(0, bufferSize: 128, format: input.inputFormatForBus(0)) { (buffer, time) -> Void in // } self.audioEngine.connect(input, to: mixer, format: input.inputFormatForBus(0)) self.audioEngine.connect(mixer, to: output, format: mixer.inputFormatForBus(0)) self.audioEngine.prepare() self.audioEngine.startAndReturnError(nil) 

When I launch this, I hear a sound, but it comes from my headphone microphone, not the guitar. How to connect to the audio coming from the lightning port?

For a quick illustration, the hardware I use here is used: Line6 Sonic Port

+5
source share
2 answers

To connect to a specific audio input, you need to set up sharedInstance AVAudioSession using the setPreferredInput:error: method.

Here's how to do it in Objective-C:

 AVAudioSession *sharedSession = [AVAudioSession sharedInstance]; NSArray *availableInputs = [sharedSession availableInputs]; [availableInputs enumerateObjectsUsingBlock:^(AVAudioSessionPortDescription *portDescription, NSUInteger idx, BOOL *stop) { if (portDescription.portType == AVAudioSessionPortUSBAudio) { [sharedSession setPreferredInput:portDescription error:nil]; } }]; 

To learn more, check:

+3
source

iOS typically routes both audio input and output from the last connected port. Since you connected the headphones, the port that it uses for recording. Turn off the headphones, turn on the lightning, and he will use this route to record sound and play.

+2
source

Source: https://habr.com/ru/post/1211681/


All Articles