Connection Addresshttp://www.samirchen.com/ios-avaudiosession-3/
The Avaudiosession class is introduced by the Avfoundation framework. Each iOS app has an audio session. This session can be accessed by the Sharedinstance class method of the Avaudiosession class, as follows
Once you have an instance of the Avaudiosession class, you can choose from the different categories available to your iOS app by invoking the Setcategory:error: instance method of the audio session object. The following is a list of audio session categories available for use [[Avaudiosession sharedinstance]
Setcategory:avaudiosessioncategoryplayback
Error: &setCategoryErr];
Set whether audio is enabled
[[Avaudiosession Sharedinstance]
Setactive:yes
Error: &activationErr];
Avaudiosessioncategorysoloambient
This category is much like the avaudiosessioncategoryambient category, except that it stops audio playback for other programs, such as an ipod program. When the device is set to silent mode, your audio playback will stop.
Avaudiosessioncategoryrecord
This stops other apps ' sounds (such as ipods) and allows your app to initialize audio playback (such as Avaudioplayer). In this mode, you can only make recordings. Using this class, calling Avaudioplayer's Preparetoplay will return yes, but calling the play method will return No. The main UI interface will work as usual. At this point, the app's recording will continue even if your device screen is locked by the user.
Avaudiosessioncategoryplayback
This class will quiesce audio playback for other applications (such as audio playback for ipod apps). You can use Avaudioplayer's preparetoplay and play methods to play sounds in your app. The main UI interface will work as usual. In this case, the audio playback will continue even if the screen is locked or the device is in silent mode.
Avaudiosessioncategoryplayandrecord
This category allows you to play and record sound at the same time in your app. When your voice recording or playback starts, other apps ' sound playback will stop. The main UI interface will work as usual. In this case, the audio playback and recording will continue even if the screen is locked or the device is in silent mode.
Avaudiosessioncategoryaudioprocessing
This category is used for audio processing in the app, not for audio playback or recording. Set this mode and you won't be able to play and record any sound in your app. Both the Preparetoplay and play methods that call Avaplayer will return No. Audio playback for other applications, such as ipod, will also stop in this mode.
Avaudiosessioncategoryambient
This category does not stop the sound of other apps, instead it allows your audio to play on top of other apps ' sounds, such as ipods. Your app's main UI county will work fine. Both the Preparetoplay and play methods that call Avaplayer will return yes. When the user locks the screen, your app will stop all audio being played back. The mute mode will stop your program's audio playback only if your app is the only app that plays the audio file. If you start playing audio while your ipod is playing a song, setting the device to silent mode does not stop your audio playback.
Custom Audio Session Category
2016-07-26
The content of this paper mainly originates from working with Categories.
For the audio Session, the Category that corresponds to it is the key information that explains its audio behavior. For example: Should your app's voice be controlled by the phone's mute key, your app will not use audio input or output, other music can coexist with your audio, and so on.
Each category specifies whether the following capabilities are supported:
Interrupts non-mixable apps audio
: Whether to interrupt apps that do not support mix playback. If so, audio from apps that don't support mix playback will be interrupted when your app's audio starts playing.
Silenced by the Silent switch
: Whether to respond to the phone mute key. If so, your audio will be muted when the phone's mute key is muted.
Supports audio input
: audio input is supported. If so, your app can record it.
Supports audio output
: Audio output is supported. If so, your app can play audio.
The following are the competency tables for each Category:
Category |
whether it will be muted by the mute key or the lock screen key |
whether to break apps that don't support mix playback |
whether to allow audio input/output |
Avaudiosessioncategoryambient |
Yes |
NO |
Output only |
Avaudiosessioncategoryaudioprocessing |
- |
YES |
No inputs and outputs |
Avaudiosessioncategorymultiroute |
NO |
YES |
Supports input and output |
Avaudiosessioncategoryplayandrecord |
NO |
Default YES, can override switch to NO |
Supports input and output |
Avaudiosessioncategoryplayback |
NO |
Default YES, can override switch to NO |
Output only |
Avaudiosessioncategoryrecord |
NO (remains recorded while lock screen) |
YES |
Enter only |
Avaudiosessioncategorysoloambient |
YES |
YES |
Output only |
Most apps only need to set the category at startup, but you can change the category of the audio session at any time, or you can activate and deactivate the audio session at any time. When your Audio Session is Inactive, the Category request is sent when you activate it, and if it is active, it is sent immediately.
Choose the most appropriate Category
The most accurate behavior definition for each category is controlled by the system rather than your application, and Apple may redefine the behavior of different category in the future, so you'd better choose a category that matches your app's audio style.
Here's a list of usage scenarios for various Category types:
Avaudiosessioncategoryambient, only audio playback is supported. For applications that use audio as an accessible element rather than an unreachable feature, your audio will be muted by the mute key and the lock screen key, using this Category.
Avaudiosessioncategorysoloambient, this is the Category that is used by default and only supports audio playback. The audio is muted by the mute key and the lock screen key. The only difference between this Category and Avaudiosessioncategoryambient is that it interrupts the audio playback of other apps.
Avaudiosessioncategoryplayback, only audio playback is supported. Your audio is not muted by the mute key and the lock screen key. You can use this Category when your app uses audio playback as an important feature.
One thing to note is that when you select the Category that supports your audio to continue playing when the mute key is cut to mute and the lock screen key is switched to the lock screen, you must have the ability to open Background audio in your app, See Uibackgroundmodes. Also, usually you should not shut down the system's Sleep Timer through the idletimerdisabled interface. If you close, then you should put idletimerdisabled back to no, so that your app will not prohibit automatic screen lock, after all, the lock screen will not affect your audio playback, why do you close it? Sleep Timer ensures that your screen is automatically dimmed and locked to save power after the user has not operated for a period of time.
- Avaudiosessioncategoryrecord, only audio recording is supported. If your app needs to be recorded and played, you should use Avaudiosessioncategoryplayandrecord.
- Avaudiosessioncategoryplayandrecord, supports audio playback and recording. Audio input and output do not need to be synchronized, of course, can also be synchronized. For audio call applications, you can use this category.
- Avaudiosessioncategoryaudioprocessing, only offline audio processing is supported. Offline audio processing is supported, and playback and recording are not supported.
- Avaudiosessioncategorymultiroute, supports audio playback and recording. Allows simultaneous input and output of multiple audio streams. For example: USB and headset simultaneous audio output.
In addition to Avaudiosessioncategorymultiroute, the other Category follows the last in wins principle, which is the final incoming audio device as the primary device for input or output.
Use Avaudiosessioncategorymultiroute to extend audio selection
Avaudiosessioncategorymultiroute does not simply follow the last in wins principle, Avaudiosessioncategorymultiroute Allows you to use all the connected output interfaces, not just the last connector. For example, when you are listening to audio through the HDMI output path and then plugged in the headset, your app can output audio both in HDMI and in the headset.
Under Avaudiosessioncategorymultiroute, your app can send different audio streams to different output paths. For example, your app can send an audio stream to your left ear, another audio stream to the right ear, and a third stream to the HDMI path. As shown in the following:
The Avaudiosessioncategorymultiroute supports the following output combinations:
- USB + Headset
- HDMI + Headset
- LineOut + Headset
The Avaudiosessioncategorymultiroute supports a single input interface.
Set Audio Session Category
The code is as follows:
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
// BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers error:&setCategoryError];
if (!success) { /* handle the error in setCategoryError */ }
Use Mode to customize Category
Just as you can customize the audio behavior of your application using category, we can use Mode to customize the behavior of the category. The optional Mode has these:
Mode |
compatible Category |
avaudiosessionmodedefault |
all |
Avaudiosessionmodevoicech At |
avaudiosessioncategoryplayandrecord |
avaudiosessionmodegamechat |
Avaud Iosessioncategoryplayandrecord |
avaudiosessionmodevideorecording |
Avaudiosessioncateg Oryplayandrecord avaudiosessioncategoryrecord |
avaudiosessionmodemovieplayback |
AVAud Iosessioncategoryplayback |
avaudiosessionmodemeasurement |
Avaudiosessioncategoryplaya Ndrecord avaudiosessioncategoryrecord avaudiosessioncategoryplayback |
Avaudiosessionmodevideocha T |
avaudiosessioncategoryplayandrecord |
In addition to these Mode, sometimes when customizing your Category, you also need to use some Option:
Option |
Description |
compatible Category |
Avaudiosessioncategoryoptionmixwithothers |
Allow and other audio mixes |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryplayback Avaudiosessioncategorymultiroute |
Avaudiosessioncategoryoptionduckothers |
Smart Low Conflict Audio volume |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryplayback Avaudiosessioncategorymultiroute |
Avaudiosessioncategoryoptionallowbluetooth |
Allow Bluetooth audio input |
Avaudiosessioncategoryrecord Avaudiosessioncategoryplayandrecord |
Avaudiosessioncategoryoptiondefaulttospeaker |
Default output Audio to Speaker |
Avaudiosessioncategoryplayandrecord |
Avaudiosessionmodedefault, the default mode can be used with all Category compatibility and configure the device for general use.
Avaudiosessionmodevoicechat, commonly used in VoIP type applications. This mode is only used for Avaudiosessioncategoryplayandrecord Category. In this mode, the signal used for the audio is optimized by the system-provided signal handler, and this mode is configured on the Avaudiosessioncategoryoptionallowbluetooth option. In this setting, when the system's built-in microphone is used, the system automatically selects the best built-in microphone combination to support voice chat, which optimizes the voice chat experience.
Avaudiosessionmodevideochat, typically used for video chat applications, such as FaceTime. This mode is only used for Avaudiosessioncategoryplayandrecord Category. In this mode, the signal used for audio is optimized by the system-provided signal handler, and this mode is configured on the Avaudiosessioncategoryoptionallowbluetooth and Avaudiosessioncategoryoptiondefaulttospeaker option. In this setting, when the system's built-in microphone is used, the system automatically selects the best built-in microphone combination to support video chat, which optimizes the video chat experience.
Apple recommends that audio or video chat applications also use the voice-processing I/O Unit, which provides a range of features to support VoIP class applications.
Avaudiosessionmodegamechat, commonly used in game applications. Apps that use the Gkvoicechat object will automatically set this mode and Avaudiosessioncategoryplayandrecord Category. This pattern uses the same path parameters as Avaudiosessionmodevideochat.
Avaudiosessionmodevideorecording, typically used for applications that require video capture by the camera. This mode is only used for the two Category of Avaudiosessioncategoryplayandrecord and Avaudiosessioncategoryrecord. In this mode, the signal is modified by the signal handler provided by the system. This mode is combined with the avcapturesession API to provide better control of the audio and video input and output paths. For example, setting the Automaticallyconfiguresapplicationaudiosession property allows the system to automatically select the optimal output path based on the device being used.
Avaudiosessionmodemeasurement is typically used for applications that minimize the impact of audio and video signal processing processes on the system's own signal. This mode is only used for Avaudiosessioncategoryplayandrecord, Avaudiosessioncategoryrecord, Avaudiosessioncategoryplayback Category. The input signal is routed by the main microphone of the device.
Avaudiosessionmodemovieplayback, typically used to play movies or other video applications. This mode is only used for Avaudiosessioncategoryplayback Category.
Supports AirPlay's Category and Mode
The following Category supports "mirrored" and "non-mirrored" versions of AirPlay:
- Avaudiosessioncategorysoloambient
- Avaudiosessioncategoryambient
- Avaudiosessioncategoryplayback
In addition
- Avaudiosessioncategoryplayandrecord only supports "mirrored" versions of AirPlay.
Mode supports AirPlay only when used in conjunction with the Avaudiosessioncategoryplayandrecord Category, and the following mode only supports the "mirrored" version of AirPlay:
- Avaudiosessionmodedefault
- Avaudiosessionmodevideochat
- Avaudiosessionmodegamechat
Category Use tuning
You can tune the Category in a number of ways, here are a few examples:
- Allow audio from other apps to play with your audio mix. You can rewrite Avaudiosessioncategoryplayback, Avaudiosessioncategoryplayandrecord, Avaudiosessioncategorymultiroute these The interrupt feature of Category. When rewriting, you need to apply the Avaudiosessioncategoryoptionmixwithothers option to your Audio Session. If your app is mixable, your app won't interrupt its audio when an audio Session of an non-mixable app is activated. Similarly, your app's audio will not be interrupted by other non-mixable apps when it plays.
- Change the audio output path from the handset to the speaker. When you use the Avaudiosessioncategoryplayandrecord Category, the audio is output from the handset by default, and you can pass the Overrideoutputaudioport:error: method to change the audio output path to the speaker.
- When your audio is playing, you can force other audio to lower the volume. When you need to apply avaudiosessioncategoryoptionduckothers to your Category, applications that use this feature need to manage their own audio session, activate the audio session before playing Close the Audio Session after playback is complete.
Audio Recording Permission request
After IOS7, the permission to record audio requires user authorization to obtain it. If the user does not give you permission, then you are recording mute. When you use a Category that requires recording permission to customize your Audio Session, the system automatically pops up the permission request prompt to the user.
In addition to prompting the system to automatically eject permission requests, you can also request permissions from the user through the Requestrecordpermission: method.
Avaudiosession Study Notes