# Setting preffered audio input on iOS

**URL:** <https://forum.juce.com/t/setting-preffered-audio-input-on-ios/25659>\
**Category:** MacOSX and iOS\
**Created:** [December 20, 2017, 12:05am UTC](https://forum.juce.com/t/setting-preffered-audio-input-on-ios/25659 "2017-12-20T00:05:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zebra1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/zebra1/32/2668_2.png) [@zebra1](https://forum.juce.com/u/zebra1)\
**Post date:** [December 20, 2017, 12:05am UTC](https://forum.juce.com/t/setting-preffered-audio-input-on-ios/25659/1 "2017-12-20T00:05:13Z")

</div>

hello,

Is there a mechanism in JUCE to change the audio input on iOS, to switch between headset mic and built-in mic?

for example in swift i can do

```auto
for portDesc in session.availableInputs! {
    if(portDesc.portType == AVAudioSessionPortBuiltInMic) {
        try session.setPreferredInput(portDesc)
    }
}

```

a quick search through `juce_ios_Audio.cpp` doesn’t reveal any glue for this, nor does a search of the forum. anyone have a solution before i try to hack my own? (i have little experience with iOS or objective-c++, truth be told, so its a little intimidating.)

apologies if i’ve missed something obvious, and thanks.

* * *

[edit] well i’ve successfully hacked in something along the lines of

```auto
AVAudioSession* session = [AVAudioSession sharedInstance];
auto inputs = [session availableInputs];
for (AVAudioSessionPortDescription* input : inputs) {
    auto type = input.portType;
    bool isInternalMic = [type isEqualToString:@"MicrophoneBuiltIn"];
    if(isInternalMic) {
        JUCE_NSERROR_CHECK ( [session setPreferredInput:input error:&error]);
    }
}

```

which seems to work ok. though, strangely, changing the session mode (e.g. toggling preprocessing) resets the selected input port to the default.

incidentally, the preprocessing mode flag is backwards in `juce_ios_Audio.cpp:setAudioPreProcessingEnabled()`:

```auto
NSString* mode = (enable ? AVAudioSessionModeMeasurement
                                 : AVAudioSessionModeDefault);

```

( `AVAudioSessionModeMeasurement` is the mode that _disables_ AGC and so on. )

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [December 20, 2017, 11:11am UTC](https://forum.juce.com/t/setting-preffered-audio-input-on-ios/25659/2 "2017-12-20T11:11:42Z")

</div>

Yes, that’s the way to go. As you’re experiencing, iOS likes to manage the input and output devices itself so you’ll need to fight to keep control of it. There are various callbacks you can listen to to find out about hardware and/or input source changes like here (on the develop branch - we’ve made significant changes since the last master release)

> <https://github.com/WeAreROLI/JUCE/blob/develop/modules/juce_audio_devices/native/juce_ios_Audio.cpp#L734>

but exactly how you should handle those to do what you want is pretty unclear. It’s easy to end up on the wrong thread or in a callback loop…

---

<div class="post-metadata">

**Author:** ![gericsapo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/gericsapo/32/2966_2.png) [@gericsapo](https://forum.juce.com/u/gericsapo)\
**Post date:** [March 12, 2018, 3:56pm UTC](https://forum.juce.com/t/setting-preffered-audio-input-on-ios/25659/3 "2018-03-12T15:56:40Z")

</div>

> [@zebra1](#):
>
> [session

Thanks for sharing this!

I’ve been following your implementation, but weirdly it overrides the output as well…  
I’m setting the preferred input in the open() method, right after the session category is selected.  
Any idea what am I possibly missing?

Thanks  
Geri
