3

I am trying to set the microphone gain with "setInputGain" in AVAudioSession to handle very weak sounds, but I am only partly successful. I am checking if "isInputGainSettable" and then I try to change the gain with a slider. I am checking if the gain actually changes, both by reading back the value and checking an actual recorded sound. The result is as follows:

enter image description here

The code I am using

-(void)viewDidLoad
{
    self.audioSession = [AVAudioSession sharedInstance];
    if(self.audioSession.isInputGainSettable){
        [self.audioSession setActive:YES error:nil];
    }
}

-(IBAction)setGain:(id)sender 
{
  float gain = self.gainSlider.value;
  NSError* error;
  BOOL gainset = [self.audioSession setInputGain:gain error:&error];
  if (!gainset) NSLog(@"failed %@", error);

  NSLog(@"audiosession gain: %.2f ",self.audioSession.inputGain);
}

I am not getting any error messages. I have been searching SO and elsewhere and people are both reporting problems, but also that they are able to set the gain on iPads and older iPhones. The only "trick" that I have seen reported is to "wait a while" before setting the gain, something I have tried without success.

So the question is if there something I have missed, and if I should be able to set the gain on iPads and older iPhones?

1 Answer 1

0

I was exhausted as well and searched for almost three hours.

The only thing which worked for me was calling the setGain method in viewDidAppear.

1
  • I've tried calling this well after viewDidAppear and it still doesn't work for me Feb 26, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.