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];
Related
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 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];
}
Sorry if this is a duplicate, but I couldn't find a question similar to this. I have a custom camera/recorder that I made with AVFoundation and I was wondering how to keep the audio running from other apps while recording a video because right now it stops the audio (doesn't even pause it) and then records the video
If I am thinking correctly, could this be solved by adding something similar to this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Sample Code:
AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
OSStatus propertySetError = 0;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
propertySetError = AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride
);
[session setActive:YES error:&error];
This fixed it.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers
error:nil];
I am implementing Audio Video Call like feature. I am using AVAudioSession for Voice feature. I want to configure the AVAudioSession such that it should work on the receive speaker and loud speaker as well as the headphones.I am using the following initialisation for AVAudioSession:
NSError *err = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&err];
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&err];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoChat error:&err];
[[AVAudioSession sharedInstance] setActive: YES error: NULL];
The above code works fine for loud speaker and headphones but the voice quality from loud speaker is very low.
I have tried the code below for switching the audio route to receiver speaker. But it doesn't work.
NSError *err = nil;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&err];
Can somebody please help me with this?
I got similar problem.i solve it by
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
NSArray *outputs =[[[AVAudioSession sharedInstance] currentRoute] outputs];
BOOL haveSpeaker = NO;
for (AVAudioSessionPortDescription *currentPut in outputs) {
if ([currentPut.portType isEqualToString:#"Speaker"]) {
haveSpeaker = YES;
break;
}
}
if (haveSpeaker) {
NSError *error;
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryRecord error:nil];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
}
In my opinion AVAudioSessionCategoryOptionDefaultNone is not reciver,it's default route. so u hava changed the default route for this mode;
I'm using AVSoundSession to configure sound, and AVAudioPlayer to play different sounds.
I searched a lot and couldn't find anything. How can I manipulate output sources?
I need a method in my SoundManager where I could switch output between phone speaker and loudspeaker.
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
Using this I can route sound to loudspeaker, but there is no method to move it to phone speaker. Can anybody help me with it?
So, I found solution for manipulating with sound output.
You could initialize sound settings with AVAudioSession
Something like this:
session = [AVAudioSession sharedInstance];
BOOL success;
NSError* error;
success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (!success) NSLog(#"AVAudioSession error setting category:%#",error);
success = [session setMode:AVAudioSessionModeVoiceChat error:&error];
if (!success) NSLog(#"AVAudioSession error setting mode:%#",error);
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
[session setPreferredOutputNumberOfChannels:0 error:nil];
if (!success) NSLog(#"AVAudioSession error overrideOutputAudioPort:%#",error);
success = [session setActive:YES error:&error];
if (!success) NSLog(#"AVAudioSession error activating: %#",error);
else NSLog(#"audioSession active");
With
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
You set don't override output port. And your app playing with default speaker. As I understand for mode AVAudioSessionModeVoiceChat used phone speaker. It's directly what I need for my SIP caller app.
Then you can override output port with
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
I do it so:
- (void) loudSpeakerOn:(BOOL)isLoudSpeaker{
[session setActive:NO error:nil];
BOOL success;
NSError* error;
success = [session overrideOutputAudioPort:isLoudSpeaker?AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:&error];
if (!success) NSLog(#"AVAudioSession error setting category:%#",error);
[session setActive:YES error:nil];
}