1. 程式人生 > 實用技巧 >Differences between AVAudioSessionPortOverrideSpeaker and AVAudioSessionCategoryOptionDefaultToSpeaker

Differences between AVAudioSessionPortOverrideSpeaker and AVAudioSessionCategoryOptionDefaultToSpeaker

https://developer.apple.com/library/archive/qa/qa1754/_index.html#//apple_ref/doc/uid/DTS40011281

Q: Can you explain the difference between calling the AVAudioSession methodoverrideOutputAudioPort:with the valueAVAudioSessionPortOverrideSpeakerand using the category optionAVAudioSessionCategoryOptionDefaultToSpeaker
withsetCategory:withOptions:error:.

A:The difference is that setting theAVAudioSessionPortOverrideby callingoverrideOutputAudioPort:is more transient than using the category optionAVAudioSessionCategoryOptionDefaultToSpeaker.

Important:The use of bothAVAudioSessionPortOverrideSpeakerandAVAudioSessionCategoryOptionDefaultToSpeaker

are only applicable to theAVAudioSessionCategoryPlayAndRecordcategory.

CallingoverrideOutputAudioPort:and setting theAVAudioSessionPortOverridetoAVAudioSessionPortOverrideSpeakeris a way of temporarily overriding the output to play to the speaker. Any route change or interruption will cause the audio to be routed back to its normal route, following the last-in wins rule. Think of usingoverrideOutputAudioPort:

in terms of what you might use to implement a Speakerphone button where you want to be able to toggle between the speaker (AVAudioSessionPortOverrideSpeaker) and the normal output route (AVAudioSessionPortOverrideNone).

Note:This property is intended to allow 3rd party applications to mimic the behavior of a Speakerphone button and therefore may change the input route as well as output route. For example, setting theAVAudioSessionPortOverridetoAVAudioSessionPortOverrideSpeakerwhile a headset is plugged in will cause the route to change to built-in mic / built-in speaker.

The preferred way to implement Speakerphone functionality is to useMPVolumeView's Route button.

In contrast,AVAudioSessionCategoryOptionDefaultToSpeakermodifies the routing behavior of theAVAudioSessionCategoryPlayAndRecordcategory so that audio willalwaysroute to the speaker rather than receiver ifnoother accessory such as headphones are in use.

When usingAVAudioSessionCategoryOptionDefaultToSpeaker, user gestures will be honored. For example, plugging in a headset will cause the route to change to headset mic/headphones and unplugging the headset will cause the route to change to built-in mic/speaker (as opposed to built-in mic/receiver) when this override has been set.

In the case of using a USB input-only accessory, audio input would come from the accessory and output would route to headphones (if attached) or speaker if headphones are not plugged in. The use case is simply to route audio to speaker instead of receiver in cases where the audio would normally go to the receiver. This option is a modifier for the category.

Route changes and interruptions willnotreset this override. Only changing theAVAudioSessioncategory will cause this option to be reset.