Routing audio input to receive from TOP microphone on iPhone

天大地大妈咪最大 提交于 2019-12-06 09:47:23

Use AVAudioSession to get available inputs. On my iPhone 5 it looks like this.

NSLog(@"%@", [AVAudioSession sharedInstance].availableInputs);

  "<AVAudioSessionPortDescription: 0x14554400, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Back>"

Then use one of these inputs to get availableDataSources, like this.

NSLog(@"%@", [AVAudioSession sharedInstance].availableInputs[0].dataSources);

  "<AVAudioSessionDataSourceDescription: 0x145afb00, ID = 1835216945; name = Bottom>",
  "<AVAudioSessionDataSourceDescription: 0x145b1870, ID = 1835216946; name = Front>",
  "<AVAudioSessionDataSourceDescription: 0x145b3650, ID = 1835216947; name = Back>"

We can see that there are actually THREE microphones on this iPhone: top front, top back, and bottom. Now you can set your preferred data source.

AVAudioSessionPortDescription *port = [AVAudioSession sharedInstance].availableInputs[0];
for (AVAudioSessionDataSourceDescription *source in port.dataSources) {
    if ([source.dataSourceName isEqualToString:@"Back"]) {
        [port setPreferredDataSource:source error:nil];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!