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.
Related
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];
}
I need to stream the voice data to server, so I can't use AVAudioRecorder.
Here is my approach.
set it Allow Bluetooth and activate it
NSError* e;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:nil];
[audioSession setActive:YES withOptions:0 error:&e];
I did setup the notification for AVAudioSessionRouteChangeNotification
so I see the input source changed to BluetoothHFP
but seems there is no way to configure a BluetoothHFP input to AVCaptureDeviceInput.
self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetLow;
AVCaptureDevice* captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput* audioInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if(audioInput && [self.captureSession canAddInput:audioInput]){
[self.captureSession addInput:audioInput];
}
after doing so, the input source changed to iPhone MicroPhone.
is there anyway I can get the BluetoothHFP input as AVCaptureDeviceInput??
I want to play audio from earpiece . I tried the solution provided in this : Play Audio but it is not working. I also tried
NSError *error = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[session setActive: YES error:nil];
AVAudioSessionPortDescription *routePort = session.currentRoute.outputs.firstObject;
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
I play audio from earpiece by writing these lines in my ViewController's viewDidLoad method:
NSError *error = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[session setActive: YES error:nil];
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
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];
}
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];