AVAudioSession SetInputGain stopped working - ios

I am using the following code to mute my audio and video recorded session. But I released it was working earlier, but now it's stopped working.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
if (audioSession.isInputGainSettable) {
if(deviceInputGainVal == 0){
deviceInputGainVal = audioSession.inputGain;
}
NSError *error = nil;
BOOL success = [audioSession setInputGain:gain error:&error];
if (!success) {
NSLog(#"%#", error);
}
}
else {
NSLog(#"Cannot set input gain");
}

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?

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.

AVCaputeDeviceInput capture bluetooth input

I know how to set the AVAudioSession to be able to read audio input from a bluetooth device. I accomplish this by the following code:
NSError *error;
[[AVAudioSession sharedInstance] setActive:NO error:&error];
[self.audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
NSError *errorTwo;
[self.audioSession setActive:YES error:&errorTwo];
By setting the options to AVAudioSessionOptionAllowBlueTooth I am able set this up. However, I'm trying to get this to work with a video recorder and I'm struggling to get this to work.
I'll post what I have starting from the AVCaptureSession:
self.captureSession = [AVCaptureSession new];
NSError *error;
[[AVAudioSession sharedInstance] setActive:NO error:&error];
self.captureSession.automaticallyConfiguresApplicationAudioSession = YES;
self.captureSession.automaticallyConfiguresApplicationAudioSession = true;
[self.audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
NSError *errorTwo;
[self.audioSession setActive:YES error:&errorTwo];
[self addAudioCapture];
AVCaptureDevice *audioInputDevice = [self audioDevice];
- (BOOL)addAudioCapture
{
AVCaptureDevice *audioInputDevice = [self audioDevice];
DLog(#"AUDIO INPUT DEVICE: %#", [audioInputDevice description]);
AVCaptureDeviceInput *audioIn = [[AVCaptureDeviceInput alloc] initWithDevice:audioInputDevice error:nil];
if ([self.captureSession canAddInput:audioIn])
{
[self.captureSession addInput:audioIn];
}
else
{
return NO;
}
self.audioOut = [[AVCaptureAudioDataOutput alloc] init];
dispatch_queue_t audioCaptureQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);
[self.audioOut setSampleBufferDelegate:self queue:audioCaptureQueue];
if ([self.captureSession canAddOutput:self.audioOut])
{
[self.captureSession addOutput:self.audioOut];
}
else
{
return NO;
}
self.audioConnection = [self.audioOut connectionWithMediaType:AVMediaTypeAudio];
return YES;
}
- (AVCaptureDevice *)audioDevice
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
if ([devices count] > 0)
{
return [devices objectAtIndex:0];
}
return nil;
}
However, the problem I'm facing is the audioInputDevice is always the iPhone microphone. I've confirmed that my bluetooth device is paired with my phone and I've written a separate application to test what input / output I'm currently using and I can confirm the the bluetooth device is readable by the iPhone. However, in the main app I'm working in I'm unable to set this. Any tips or advice on how to achieve this would be appreciated.

incompatible pointer types sending "NSString" to parameter of type 'AVAudioSessionPortDescription*

Trying to use only builtIn iPhone MIC for my app.
- (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort error:(NSError **)outError NS_AVAILABLE_IOS(7_0);
{
NSError* audioError = nil;
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
[myAudioSession setPreferredInput:AVAudioSessionPortBuiltInMic error:&audioError];
AVAudioSessionPortBuiltInMic is an NSString, not an AVAudioSessionPortDescription. In order to get the descriptors for the available ports on the device, use [AVAudioSession availableInputs].
for (AVAudioSessionPortDescription *input in [AVAudioSession availableInputs]) {
if ([[input portType] isEqualToString:AVAudioSessionPortBuiltInMic]) {
NSError *error = nil;
[AVAudioSession setPreferredInput:input error:&error];
}
}

How to programmatically mute video

How to play a video without sound?
I have tried these options but neither of them worked:
1) Setting the MPMusicPlayerController player to ipodMusicPlayer and disabling the application audio.
musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0];
2) Forcing the app to run the audio
NSError *error = nil;
if ([[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&error]) {
NSLog(#"Error setting audio session: %#", error);
}
Use this codebase to mute.
NSError *errorFeedBack = nil;
if (![[AVAudioSession sharedInstance] setActive:NO error:&errorFeedBack] )
{
NSLog(#"Error encountered: %#", [errorFeedBack localizedDescription]);
}

Resources