覆盖以仅使用设备的内部麦克风

Override to use a device's internal microphone only

即使连接了带麦克风的耳机,我也需要使用内置 iPhone 麦克风。如何以编程方式设置它?

摘自Apple Docs:

Sets the preferred input port for audio routing.

func setPreferredInput(_ inPort: AVAudioSessionPortDescription?) throws

Setting the preferred input port requests a change to the input audio route. To determine whether the change takes effect, use the currentRoute property. The value of the inPort parameter must be one of the AVAudioSessionPortDescription objects in the availableInputs array. If this parameter specifies a port that is not already part of the current audio route and the app’s session controls audio routing, this method initiates a route change to use the preferred port.You must set a preferred input port only after setting the audio session’s category and mode and activating the session.

采纳了 Sivajee Battina 的建议来解决问题。这就是我的工作方式:

NSArray *inputs = [audioSession availableInputs];
for (AVAudioSessionPortDescription *desc in inputs)
{
    NSString *portType = [desc portType];
    if ([portType isEqualToString: AVAudioSessionPortBuiltInMic])
    {
        [audioSession setPreferredInput: desc error: &error];
        break;
    }
}

仍然将他的答案标记为正确答案。