Need to use BuiltInMic after connecting headphones also for iOS - ios

I am making an app in which i always want to take input from the device (iOS) BuiltInMic even if the headphones are plugged in. I have done a lot of research on it and came out with some solutions but they are not working.
I need to use this method to get the preferred input from a device :-
setPreferredInput:error:
More description can be found on this link :-
https://developer.apple.com/library/ios/qa/qa1799/_index.html
I got stuck in the method parameters to be passed
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setPreferredInput:(AVAudioSessionPortDescription *)inPort error:&myAudioError];
i.e the inPort to be pass. I need to set it to AVAudioSessionPortBuiltInMic but i am not able to do it.
Any help would be appreciated.

From Api Documents
- (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride
error:(NSError **)outError
Calling this method with the AVAudioSessionPortOverrideSpeaker option and the audio session’s category AVAudioSessionCategoryPlayAndRecord causes audio to use the built-in speaker and microphone regardless of other settings. This change remains in effect only until the current route changes or you call this method again with the AVAudioSessionPortOverrideNone option.

Related

Use system channel instead of ringer channel when using EPSSampler in iOS

I'm using EPSSampler to play some notes in my tiny iOS app. It works nicely, except that the sound is controlled through the ringer settings instead of the system volume settings. The problem is that, if the ringer is turned off, my app makes no sound.
The closest I could find was this SO question but I can't quite translate that solution into mine, as I can't match it to EPSSampler's code. Any insight?
EDIT To clarify, the only code that deals specifically with audio is contained in the EPSSampler class, my app merely instances it, loads an AUSampler preset, and calls the note-playing methods in EPSSampler, so a solution would need to change something in EPSSampler rather than in my app -- that's why I'm not posting any source code.
Turns out this had nothing to do with EPSSampler, and everything with configuring the audio session. The following code did the trick.
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
That'll teach me to RTFM.
For swift(Use when initialising the music):
_ = try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)

Do not allow other apps play music

We are developing a VOIP application and while there is an active call, we want not to allow other apps play music or something (like Whatsapp does). Is there any way of doing it in a normal way, can you help please?
Edit 1: I think we need to start with listening the observer AVAudioSessionInterruptionNotification.
Accepted Answer Edit 2:
When sound interruption begins, try to only active the AudioSession again. This works. Thank you KudoCC.
Here is a link talks about audio category, there is a table in it:
As your app supports VoIP, so it need to play and record audio, and you need interrupt non-mixable apps audio, so AVAudioSessionCategoryPlayAndRecord is the right category. Also you should re-active your audio session when you're playing/recording audio so that the category takes effect.
Try this code in your app to pause the background audio when calls,
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
It is helpful when other apps uses AVAudioPlayer.
For more detail refer this Link

Connecting to car bluetooth system causes to self phone call from device

We write app that records audio in background. We use AVAudioSession with AVAudioSessionCategoryPlayAndRecord category and AVAudioSessionCategoryOptionAllowBluetooth option:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions: AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth error:&error];
We noticed the strange issue - when iPhone connected to Multimedia Car Bluetooth system and our app runs, there are shown phone call from device to itself (the call continues during all device connection to bluetooth system). When we close app - the call is ended, when we open app again - call begins and etc.
This issue reproduces on all Multimedia Bluetooth systems. How can we fix it? Thanks
Think you need to add AVAudioSessionCategoryOptionAllowBluetoothA2DP instead of the old HFP profile. The call is used to make older bluetooth devices to work.
With iOS 10, Apple added the option AVAudioSessionCategoryOptionAllowBluetoothA2DP. They also changed the meaning of AudioSessionCategoryOptionAllowBluetooth to only allow output using the HFP Bluetooth profile, which is where you get the low quality audio output.
If you use this new option in place of the AudioSessionCategoryOptionAllowBluetooth option in your code snippet, it will allow high quality output but disallow low quality audio output.
Here's the online documentation for the options, but unfortunately there's no description for the new iOS 10 options online. You can see more detail in the in-code documentation in AVAudioSession.h
https://developer.apple.com/reference/avfoundation/avaudiosessioncategoryoptions?language=objc
Info from: Keep bluetooth sound when initializing AVAudioSession

AVAudioSession mode switch volume difference

I'm trying to combine media playback with VoIP feature (via Twilio) for iOS 9 and 8.While an audio stream plays in the background, I connect or disconnect a Voice Conference session which results in a volume jump from value X to value Y. This jump can be heard, as well as observed by a [AVAudioSession sharedInstance].outputVolume value change.I would like to prevent this jump and keep the volume at a constant level, unless the user manually decides to change it.Further investigation showed that while AVAudioSession's category is set to AVAudioSessionCategoryPlayAndRecord, switching between modes[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:&error]and[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVoiceChat error:&error]causes the app to operate in two completely separate volume scales, respectively.i.e there a volume for Mode "Default" and a completely unrelated volume for Mode "Voice Chat".AVAudioSession's documentation seems to omit any mention of volume in relation to mode/category switches and I can't find anything relevant on the interwebs...
Appreciate any help.
When setting your play and record category, pass AVAudioSessionCategoryOptionDefaultToSpeaker as an option:
[[AVAudioSession sharedInstance] AVAudioSessionCategoryPlayAndRecord withOptions: AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
This overrides the default play-and-record behaviour of switching from the speaker to the much quieter receiver. The reason for this being that play-and-record was designed for telephony, where you'd be holding the phone to year ear & presumably wouldn't want to have your hearing damaged by loud sounds.
Megan from Twilio here.
I'm not most familiar with the iOS SDK but you should be able to control connection audio from TCDevice parameters incomingSoundEnabled, outgoingSoundEnabled, and disconnectSoundEnabled as documented here.
Otherwise, I would suggest looking at the sharedInstance properties of AVAudioSession that the Twilio SDK calls upon as demonstrated in this post:
setCategory:error:
setActive:error:
overrideOutputAudioPort:error:
Please let me know if this helps.

Unity sound not working after AVAudioSession is disabled

I'm currently working on a Unity3D iOS plugin. I'm trying to change the category of the app's AVAudioSession in order to implement audio ducking (i.e. Music app volume goes down while my sound effects are playing), which requires the AVAudioSession to be set as inactive and then as active again.
Well, after I set the session as inactive by using [[AVAudioSession sharedInstance] setActive:NO error:nil], Unity sounds simply won't work anymore, even after the session is set as active again. Native sounds still work, as I tested an AVAudioPlayer and it works perfectly.
Any idea on what's wrong?
User daniel_liu from Unity Answers helped me with this: Instead of setting the AVAudioSession as active/inactive by using the code I posted in the question, one should use UnitySetAudioSessionActive(true) and UnitySetAudioSessionActive(false).

Resources