iOS内置麦克风选择方法
在avaudiosession使用中:
模式中的AVAudioSessionModeVoiceChat用于VoIP是由系统进行默认选择的最适合的麦克风
模式中的AVAudioSessionModeVideoRecording默认选择上麦克风,离摄像头最近的那个,主要用于VOIP
上边只是个人测试结果,如有不妥,请帮助指正
-------------------------------------------------
关于内置麦克风用户设置
官方文档设置麦克风问题的回答:
AVAudioSession - Microphone Selection
Important: This document is no longer being updated. For the latest information about Apple SDKs, visit the documentation website.
Q: How can I choose a specific built-in microphone when recording?
A: iOS 6 automatically selects the choice of built-in microphone (on devices that have two or more built-in microphones) through the use of audio session modes. Modes affect possible routes and the digital signal processing used for input. It is important to note that they are optimized for the use case specified by each mode and setting a mode may also affect other aspects of the route being used. For example, when recording video setting the AVAudioSessionModeVideoRecording audio session mode will select the "top" microphone instead of the default "bottom" microphone on iPhone 4/4S, and on iPhone 5 the "front" and "back" microphones will be used to provide directional noise reduction through beam forming processing.
iOS 7 offers developers more flexibility in terms of selecting specific built-in microphones.
Using APIs introduced in iOS 7, developers can perform tasks such as locating a port description that represents the built-in microphone, locating specific microphones like the "front", "back" or "bottom", setting your choice of microphone as the preferred data source, setting the built-in microphone port as the preferred input and even selecting a preferred microphone polar pattern if the hardware supports it. See AVAudioSession.h.
API Summary - Preferred Inputs, Microphone Selection and Configuration
Important: Applications should set their audio session category and mode then activate the audio session prior to using any of the input selection features.
availableInputs Property
To discover what input ports are connected (or built-in) use the AVAudioSession property availableInputs. This property returns an NSArray of AVAudioSessionPortDescription objects.
/* Get the set of input ports that are available for routing. Note that this property only applies to the session's current category and mode. |
For example, if the session's current category is AVAudioSessionCategoryPlayback, there will be no available inputs. */ |
@property(readonly) NSArray * availableInputs NS_AVAILABLE_IOS(7_0); /* NSArray of AVAudioSessionPortDescription objects. */ |
setPreferredInput:error: Method
Ports (AVAudioSessionPortDescription objects) can be identified by their portType property, for example AVAudioSessionPortBuiltInMic, AVAudioSessionPortHeadsetMic and so on. See AVAudioSession.h for further details.
To set a preferred input port (built-in mic, wired mic, USB input, etc.) use the AVAudioSession setPreferredInput:error: method. This method takes a AVAudioSessionPortDescription object.
/* Select a preferred input port for audio routing. If the input port is already part of the current audio route, this will have no effect. |
Otherwise, selecting an input port for routing will initiate a route change to use the preferred input port, provided that the application's |
session controls audio routing. */ |
- (BOOL) setPreferredInput:(AVAudioSessionPortDescription*)inPort error:(NSError **)outError NS_AVAILABLE_IOS(7_0); |
dataSources Property
For ports that support data sources (built-in microphone, some USB accessories), applications can discover what data sources are available by querying the AVAudioSessionPortDescription's dataSources property. In the case of "built-in microphone", the returned description represents each individual microphone. Different devices will return different data source information. The iPhone 4 and 4S have two microphones; "bottom" and "top". The iPhone 5 has 3 microphones; "bottom", "front", and "back".
/* Array of AVAudioSessionDataSourceDescription objects. Will be nil if there are no selectable data sources. */ |
@property(readonly) NSArray * dataSources NS_AVAILABLE_IOS(7_0); |
Individual built-in microphones may be identified by a combination of a AVAudioSessionDataSourceDescription's location property (AVAudioSessionLocationUpper, AVAudioSessionLocationLower) and orientation property (AVAudioSessionOrientationTop, AVAudioSessionOrientationFront and so on). See AVAudioSession.h for further details.
setPreferredDataSource:error: Method
Applications may set a preferred data source by using the setPreferredDataSource:error: method of a AVAudioSessionPortDescription object. This method takes a AVAudioSessionDataSourceDescription object.
/* Select the preferred data source for this port. The input dataSource parameter must be one of the dataSources exposed by the dataSources property. |
Note: if the port is part of the active audio route, changing the data source will likely |
result in a route reconfiguration. If the port is not part of the active route, selecting a new data source will |
not result in an immediate route reconfiguration. Use AVAudioSession's setPreferredInput:error: method to activate the port. */ |
- (BOOL) setPreferredDataSource:(AVAudioSessionDataSourceDescription *)dataSource error:(NSError **)outError NS_AVAILABLE_IOS(7_0); |
supportedPolarPatterns Property
Some iOS devices support getting and setting microphone polar patterns for some of the built-in microphones. The iPhone 5 supports setting the preferred polar pattern for the "front" and "back" built-in microphones. Available patterns are returned using the supportedPolarPatternsproperty of a AVAudioSessionDataSourceDescription. This property will either return an array of supported polar patterns for the data source, for example AVAudioSessionPolarPatternCardioid, AVAudioSessionPolarPatternOmnidirectional and so on, or nil when no selectable patterns are available.
/* Array of one or more NSStrings describing the supported polar patterns for a data source. Will be nil for data sources that have no selectable patterns. */ |
@property(readonly) NSArray * supportedPolarPatterns NS_AVAILABLE_IOS(7_0); |
setPreferredPolarPattern:error: Method
If the data source has a number of supported polar patters, you can set the preferred polar pattern by using the AVAudioSessionDataSourceDescription's setPreferredPolarPattern:error: method.
/* Select the desired polar pattern from the set of available patterns. Note: if the owning port and data source are part of the active audio route, |
changing the polar pattern will likely result in a route reconfiguration. If the owning port and data source are not part of the active route, |
selecting a polar pattern will not result in an immediate route reconfiguration. Use AVAudioSession's setPreferredInput:error: method |
to activate the port. Use setPreferredDataSource:error: to active the data source on the port. */ |
- (BOOL) setPreferredPolarPattern:(NSString *)pattern error:(NSError **)outError NS_AVAILABLE_IOS(7_0); |
Listing 1 demonstrates how applications can find the AVAudioSessionPortDescription that represents the built-in microphone, locate the front microphone (on iPhone 5 or another device that has a front facing microphone), set the front microphone as the preferred data source and set the built-in microphone port as the preferred input.
Listing 1 Demonstrate Input Selection.
#import <AVFoundation/AVAudioSession.h> |
- (void) demonstrateInputSelection |
{
|
NSError* theError = nil; |
BOOL result = YES; |
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance]; |
result = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&theError]; |
if (!result) |
{
|
NSLog(@"setCategory failed"); |
} |
result = [myAudioSession setActive:YES error:&theError]; |
if (!result) |
{
|
NSLog(@"setActive failed"); |
} |
// Get the set of available inputs. If there are no audio accessories attached, there will be |
// only one available input -- the built in microphone. |
NSArray* inputs = [myAudioSession availableInputs]; |
// Locate the Port corresponding to the built-in microphone. |
AVAudioSessionPortDescription* builtInMicPort = nil; |
for (AVAudioSessionPortDescription* port in inputs) |
{
|
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) |
{
|
builtInMicPort = port; |
break; |
} |
} |
// Print out a description of the data sources for the built-in microphone |
NSLog(@"There are %u data sources for port :\"%@\"", (unsigned)[builtInMicPort.dataSources count], builtInMicPort); |
NSLog(@"%@", builtInMicPort.dataSources); |
// loop over the built-in mic's data sources and attempt to locate the front microphone |
AVAudioSessionDataSourceDescription* frontDataSource = nil; |
for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources) |
{
|
if ([source.orientation isEqual:AVAudioSessionOrientationFront]) |
{
|
frontDataSource = source; |
break; |
} |
} // end data source iteration |
if (frontDataSource) |
{
|
NSLog(@"Currently selected source is \"%@\" for port \"%@\"", builtInMicPort.selectedDataSource.dataSourceName, builtInMicPort.portName); |
NSLog(@"Attempting to select source \"%@\" on port \"%@\"", frontDataSource, builtInMicPort.portName); |
// Set a preference for the front data source. |
theError = nil; |
result = [builtInMicPort setPreferredDataSource:frontDataSource error:&theError]; |
if (!result) |
{
|
// an error occurred. Handle it! |
NSLog(@"setPreferredDataSource failed"); |
} |
} |
// Make sure the built-in mic is selected for input. This will be a no-op if the built-in mic is |
// already the current input Port. |
theError = nil; |
result = [myAudioSession setPreferredInput:builtInMicPort error:&theError]; |
if (!result) |
{
|
// an error occurred. Handle it! |
NSLog(@"setPreferredInput failed"); |
} |
} |
Listing 1 will produce the following console output when run on an iPhone 5:
There are 3 data sources for port :"<AVAudioSessionPortDescription: 0x14d935a0, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>" |
( |
"<AVAudioSessionDataSourceDescription: 0x14d93800, ID = 1835216945; name = Bottom>", |
"<AVAudioSessionDataSourceDescription: 0x14d938d0, ID = 1835216946; name = Front>", |
"<AVAudioSessionDataSourceDescription: 0x14d93a10, ID = 1835216947; name = Back>" |
) |
Currently selected source is "Bottom" for port "iPhone Microphone" |
Attempting to select source "<AVAudioSessionDataSourceDescription: 0x14d938d0, ID = 1835216946; name = Front>" on port "iPhone Microphone” |
Note: While the focus of this Q&A is input and microphone selection for recording, a few details about output routing are worth mentioning when the audio session category is specifically AVAudioSessionCategoryPlayAndRecord.
To change the output side of the audio route, applications may include a MPVolumeView to easily give users access to the route picker.
Applications may set the audio session option AVAudioSessionCategoryOptionDefaultToSpeaker or use the AVAudioSessionPortOverrideSpeaker override for speakerphone functionality. See Q&A QA1754 for details.
The preferred method for overriding to the speaker instead of the receiver for speakerphone functionality is through the use of MPVolumeView.
If an application uses the setPreferredInput:error: method to select a Bluetooth HFP input, the output will automatically be changed to the Bluetooth HFP output. Moreover, selecting a Bluetooth HFP output using the MPVolumeView's route picker will automatically change the input to the Bluetooth HFP input. Therefore both the input and output will always end up on the Bluetooth HFP device even though only the input or output was set individually.
Document Revision History
| Date | Notes |
|---|---|
| 2014-01-21 |
Editorial |
| 2013-09-03 |
New document that describes how to choose a specific microphone "Front", "Bottom", "Rear" and so on when available on a device. |
iOS内置麦克风选择方法的更多相关文章
- iOS内置图片瘦身思路整理
一.前言 前段时间注意到我们APP的包大小超过100MB了,所以随口跟老板说了下能否采用字体文件(.ttf)替代PNG图片,老板对应用瘦身很感兴趣因此让我做下技术调研.这篇文章主要是将我们的各个技术方 ...
- iOS 内置图片瘦身
一.iOS 内置资源的集中方式 1.1 将图片存放在 bundle 这是一种很常见的方式,项目中各类文件分类放在各个 bundle 下,项目既整洁又能达到隔离资源的目的.采用 bundle 的加载方式 ...
- IOS内置safari浏览器日期字符串转Date对象失败
代码示例: <html> <head> <title>Date字符串转化示例</title> </head> <body> &l ...
- JavaScript (内置对象及方法)
JavaScript中的对象分为3种:内置对象.浏览器对象.自定义对象 JavaScript 提供多个内置对象:Math/Array/Number/String/Boolean... 对象只是带有属性 ...
- javaScript中Math内置对象基本方法入门
概念 Math 是javaScript的内置对象,包含了部分数学常数属性和数学函数方法. Math 不是一个函数对象,用户Number类型进行使用,不支持BigInt. Math 的所有属性与方法都是 ...
- 利用jQuery内置的data()方法存储数据
jQuery提供了内置的data()方法,与DOM元素不同的是,它可以用来存储key/value类型的数据.数据的存储是很容易的: $('#myDiv').data('currentState', ' ...
- jsp内置对象的方法
JSP内置对象的方法:out:out.print();request:request对象主要用于出列客户端请求. 常用方法: String getParameter(String name) ...
- JS高级——扩展内置对象的方法
基本概念 内置对象有很多,几个比较重要的:Math.String.Date.Array 基本使用 1.内置对象创建出来的对象使用的方法使用的其实都是内置对象的原型对象中的方法 (1)a并没有charA ...
- 一个禁用mac内置键盘的方法
一个禁用mac内置键盘的方法 强大的 karabiner, 非常好用. 可以直接在有外接键盘连接的情况下, 禁用掉内置键盘 另外一个方法是启用mac的 鼠标键, 感觉用处不是很大, 修饰健并没有被禁用 ...
随机推荐
- 菜鸟的java代码审计之旅-0之java基础知识
前言: 对于java的代码审计我就是一个小白,没有代码基础(不会java),从0开始记录我的java漏洞的审计学习之旅.对于java来说是一门很难的语言,但是不去学习就永远不会.对于一门很复杂的语言如 ...
- 基于ssd的手势识别模型(object detection api方式)
[Tensorflow]Object Detection API-训练自己的手势识别模型 1. 安装tensorflow以及下载object detection api 1.安装tensorflow: ...
- linux 安装中文支持
下载 fonts-chinese-3.02-12.el5.noarch.rpm fonts-ISO8859-2-75dpi-1.0-17.1.noarch.rpm 安装各种提示的依赖 安装 chkf ...
- tomcat和iis共用80端口的简明手册
对于使用tomcat-connector实现iis与tomcat实现80端口共用的问题,网上的信息异常混乱,很多地方误人子弟,浪费时间.本文给出简明手册式的做法: 首先列出我们需要做的事项: 1. ...
- JUnit报告美化——ExtentReports
美化后效果 美化后的报告,页面清晰简洁.重要信息都可以体现出来,用例通过率,失败的用例和失败原因 主要技术点 ExtentReports JUnit的@Rule 重写TestWatcher的succe ...
- tensorflow/threading 用到的一些函数
---恢复内容开始--- import tensorflow as tf 1 tf.squeeze([1,2,3,4]) 删除所有为1的维度 eg shape从(1,2,3,1)到(2,3 ...
- WPF DEV gridcontrol 自定义计算列(TotalSummary)
/// <summary> /// 自定义计算列 /// </summary> /// <param name="sender"></pa ...
- Python的闭包和装饰器
什么是闭包 python中函数名是一个特殊的变量,它可以作为另一个函数的返回值,而闭包就是一个函数返回另一个函数后,其内部的局部变量还被另一个函数引用. 闭包的作用就是让一个变量能够常驻内存. def ...
- IIS日志分析工具-Log Parser
下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=24659 参考链接: https://www.cnblogs.com/fu ...
- sshj 示例
sshj 示例 开发常常需要去服务器做一些操作,比如配置一下,或者取服务器的配置什么的,需要写点工具方便开发. 下面是一个使用sshj 模拟ssh的过程. package sshStuff; impo ...