iOS内置麦克风选择方法 - 邗影
在avaudiosession使用中:
模式中的AVAudioSessionModeVoiceChat用于VoIP是由系统进行默认选择的最适合的麦克风
模式中的AVAudioSessionModeVideoRecording默认选择上麦克风,离摄像头最近的那个,主要用于VOIP
上边只是个人测试结果,如有不妥,请帮助指正
————————————————-
关于内置麦克风用户设置
官方文档设置麦克风问题的回答:
AVAudioSession – Microphone Selection
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
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.
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.
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”.
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.
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 supportedPolarPatterns
property 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.
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.
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 will produce the following console output when run on an iPhone 5:
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. |