为VOIP呼叫打开/关闭MIC

时间:2017-04-25 10:58:51

标签: ios swift3 voip twilio-click-to-call

我正在开发VOIP应用程序,我想实现呼叫控制面板。 我已经成功实现了扬声器/扬声器功能。 但是在VOIP呼叫时无法打开/关闭麦克风。

我尝试了以下代码:

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)
    }

1 个答案:

答案 0 :(得分:0)

试试这个链接: 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]);