3

I am working on a VoIP app and I want to implement a call control panel. I have implemented the speaker/loud-speaker functionality successfully. But failed to turn on/off the mic while on VoIP call.

I tried below code:

muteButton.setImage(UIImage(named:"mute_icon"), for: .normal)

do {       
    let audioSession = AVAudioSession.sharedInstance()
    if audioSession.isInputGainSettable {
        try audioSession.setInputGain(0.0)
    }      
} catch {
    NSLog(error.localizedDescription)
}
Marco
  • 1,572
  • 1
  • 10
  • 21
krish
  • 3,856
  • 2
  • 24
  • 28

1 Answers1

-1

Try this link : https://forums.developer.apple.com/message/64086#64086

 // activate the audio session  
[[AVAudioSession sharedInstance] setActive:YES error:&error];  
if (error) NSLog(@"ERROR SETTING SESSION ACTIVE! %ld", (long)error.code);  

// select the built-in Back mic if available  
NSArray *portDescriptions = sessionInstance.availableInputs;  
AVAudioSessionPortDescription* builtInMicPort = nil;  
AVAudioSessionDataSourceDescription* backDataSource = nil;  

NSLog(@"  availableInputs:\n");  
NSLog(@"%@", portDescriptions);  

for (AVAudioSessionPortDescription* port in portDescriptions) {  
    if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {  
        builtInMicPort = port;  
        break;  
    }  
} // end input iteration  

NSLog(@"  port Info:\n");  
NSLog(@"%@", builtInMicPort.portType);  
NSLog(@"%@", builtInMicPort.portName);  
NSLog(@"%@", builtInMicPort.channels);  

NSLog(@"  dataSources:\n");  
NSLog(@"%@", builtInMicPort.dataSources);  
NSLog(@"%@", builtInMicPort.preferredDataSource);  
NSLog(@"%@", builtInMicPort.selectedDataSource);  

if (builtInMicPort) {  
    for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources) {  
        if ([source.orientation isEqual:AVAudioSessionOrientationBack]) {  
            backDataSource = source;  
            break;  
        }  
    } // end data source iteration  

    if (backDataSource) {  
        NSError* theError = nil;  
        result = [builtInMicPort setPreferredDataSource:backDataSource error:&theError];  
        if (result) {  
            if (error) NSLog(@"ERROR SETTING PREFERRED DATA SOURCE! %ld", (long)error.code);  
        }  
    }  
}  

NSLog(@"Current route:\n");  
NSLog(@"%@", [[AVAudioSession sharedInstance] currentRoute]);  
Ruhi
  • 176
  • 2
  • 16
  • My question is to turn on/off the mic, i think your code is to get the mic type back of bottom. – krish Apr 26 '17 at 06:53
  • checkout this link https://developer.apple.com/reference/avfoundation/avaudiosession – Ruhi Apr 26 '17 at 10:18
  • Downvoted. A. this is not a good answer because it doesn't explain what needs to be done and how the code implements this (and the top link with possible explanation is not accessible). B. after analyzing I found that this code does not indeed mute the microphone, instead it sets the input source of the built-in mic to the background noise cancelling microphone. This won't work to mute the input for a variety of reasons. C. the link in the comments is just the `AVAudioSession` reference that does not include any relevant information. – Guss Jun 12 '22 at 13:02