BlueTooth audio capturing using airpod microphone - ios

How to set audio session category so that external Bluetooth airpod device can record audio using airpod microphone and works flowless with other bluetooth device as well. Here is my code.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
/*
optimum to minimize delay, must put a software resampler to deal with 8khz
*/
NSError *error;
[audioSession setPreferredSampleRate:8000.0 error:&error];
if([[UIDevice currentDevice] systemVersion].floatValue>=10.0)
{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetoothA2DP
error:&error];
}
else
{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:&error];
}

Related

iOS: AVAudioSession sample rate is not changing when playing a sound from speaker

When setting the preferred sample rate to 16000 it is working fine. However, when I want to play the sound from the speaker and print the sample rate I find it 48000.
I tried to set the sample rate again after playing from the speaker, but it is not working.
NSError *err = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO error:&err];
BOOL successfullySatSampleRate = [session setPreferredSampleRate:SAMPLING_FREQUENCY error:&err];
if(err != nil)
{
NSLog(#"Error in setting up audio: %#", err);
}
if(!successfullySatSampleRate)
{
NSLog(#"Error in setting up sample rate");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
[session setActive:YES error:&err];
NSLog(#"initialize Session preferredSampleRate = %f, hardware sampleRate = %f",[session preferredSampleRate],[session sampleRate]);
What should I do to keep the sample rate 16000?

Can we enable iPhone Speaker On before Calling using NSUrl("tel:") scheme

Can we enable iPhone speaker on before calling using NSUrl("tel:") scheme in objective c and Xamarin.iOS? If so then how we can achieve this? Thanks.
I don't believe Apple exposes any public API to do this. The user must choose the audio out channel.
To enable speaker while calling for open in Objective C, you can do something like this :
- (void)setAudioOutputSpeaker:(BOOL)enabled
{
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[session setMode:AVAudioSessionModeVoiceChat error:&error];
if (enabled) // Enable speaker
{
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
}
else // Disable speaker
{
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
}
[session setActive:YES error:&error];
}

iOS app sound only coming through headphones or when ringer not muted

I am using in my
viewDidLoad:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
if (!success) { /**handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /**handle the error condition */ }
NSURL *SoundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"music1" ofType:#"caf"]];
and have initialized in the interface
AVAudioPlayer *playSoundURL1;
and called in my method
[playSoundURL1 play];
Sound plays perfectly fine when I'm using headphones when ringer is silent or not silent. However, without any headphones I can only hear the sound when the ringer is not silenced.

iOS How to play alert sound in speaker when recording

I want to manually trigger to play some alert sound during recording audio, and I want the alert sound be recorded. So I want the alert play loudly in speaker instead of Receiver during recording audio.
I use AVAudioRecorder to record audio and use AVAudioPlayer to play the alert sound.
Code for recorder
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryRecord error:&sessionError];
if(session == nil)
NSLog(#"Error creating session: %#", [sessionError description]);
else
[session setActive:YES error:nil];
recorder = [[AVAudioRecorder alloc] initWithURL:_recordedFile settings:recordSettings error:nil];
[recorder prepareToRecord];
[recorder record];
Code for player
AVAudioSession *session = [AVAudioSession sharedInstance];
// session.outputDataSources
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
// use the louder speaker
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (audioRouteOverride),&audioRouteOverride);
if(session == nil)
NSLog(#"Error creating session: %#", [sessionError description]);
else
[session setActive:YES error:nil];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.filePath error:&error];
player.delegate =self;
player.volume = 1.0;
player.numberOfLoops = 0;
self.timeDuration = player.duration;
If I stop the recording, and manually trigger the play alert, it plays in speaker.
But during recording, when I manually trigger the play alert, nothing happens.
Is there anything wrong here? Or Is it possible to play sound in speaker when recording?
Just change category as below thats all
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

Route output to bluetooth

For an AvAudioSession I am able to switch between speaker and earpiece using the
// To speaker
AVAudioSession *session = [AVAudioSession sharedInstance];
BOOL enableSpeaker = [session overrideOutputAudioPort:AvAudioSessionPortOverrideSpeaker error:&error];
// To earpiece
AVAudioSession *session = [AVAudioSession sharedInstance];
BOOL enableSpeaker = [session overrideOutputAudioPort:AvAudioSessionPortOverrideNone error:&error];
Is there a way to route to bluetooth if it is connected. The AudioSessionSetProperty is deprecated so do not want to use that.I need a way to switch between speaker, earpiece and bluetooth.

Resources