1. Avaudiosession Overview
The last year has been doing iOS client development for IPC camera. To deal with audio, you have to figure out
Avaudiosession.
First look at the Apple's official map:
Audio Session
You can see that avaudiosession is used to manage the resource usage of multiple apps on audio hardware devices (microphones, speakers).
For example, avaudiosession can do these things.
- Set whether your app is present with other app audio, or interrupts other app sounds
- Whether your app's audio can play a sound when your phone is in silent mode
- Phone or other app to interrupt your app's audio event handling
- Devices that specify audio input and output (such as the handset output sound, or the speaker output sound)
- Whether recording is supported, audio playback is supported at the same time
2. Avaudiosession Category
The Avaudiosession interface is relatively simple. When the app starts, it automatically activates Avaudiosession, and of course we can manually activate the code below.
//导入头文件 #import <AVFoundation/AVFoundation.h> //AVAudioSession是一个单例类 AVAudioSession *session = [AVAudioSession sharedInstance]; //AVAudioSessionCategorySoloAmbient是系统默认的category [session setCategory:AVAudioSessionCategorySoloAmbient error:nil]; //激活AVAudioSession [session setActive:YES error:nil];
You can see the settings session here are two parameters, category and options
There are currently seven types under category iOS, and each category corresponds to support the following four capabilities
- Interrupts non-mixable Apps audio: Break apps that don't support remix playback
- Silenced by the Silent switch: will respond to the phone mute key switch
- Supports Audio Input: support for recording
- Supports Audio output: Whether or not playback is supported
The following charts are used to visually visualize each category's specific competency set
Category |
whether to allow audio playback/recording |
whether to interrupt other not supported remix apps |
whether mute or lock screen is muted |
avaudiosessioncategoryambient |
support play only |
no |
Yes |
avaudiosessioncategoryaudioprocessing |
does not support playback, recording is not supported |
Yes |
no |
avaudiosessioncategorymultiroute |
support playback, support recording |
Yes |
no |
Avaudiosessioncategoryplayandrecord |
support playback, support recording |
default Yes, can be overridden as no |
no |
avaudiosessioncategoryplayback |
only supports playback |
default Yes, can be overridden as no |
no /td> |
avaudiosessioncategoryrecord |
only supports recording |
Yes |
No (can still be recorded under lock screen) |
Avaudiosessioncategorysoloambient |
only supports playback |
Yes |
Yes |
Avaudiosessioncategoryambient, only audio playback is supported. In this Category, the audio is muted by the mute key and the lock screen key. And does not interrupt audio playback for other apps.
Avaudiosessioncategorysoloambient, this is the default Category used by the system and only supports audio playback. The audio is muted by the mute key and the lock screen key. Unlike Avaudiosessioncategoryambient, this interrupts the audio playback of other applications.
Avaudiosessioncategoryplayback, only audio playback is supported. Your audio is not muted by the mute key and the lock screen key. Apply to audio is the main function of the app, such as NetEase cloud these music app, lock screen can still play.
It is important to note that the support background audio function must be turned on in the app when the audio Category can still be played when the mute key is cut to mute and the lock screen key is cut to the lock screen, see Uibackgroundmodes.
- Avaudiosessioncategoryrecord, only audio recording is supported. Playback is not supported.
- Avaudiosessioncategoryplayandrecord, supports audio playback and recording. The input and output of the audio do not need to be synchronized, or they can be synchronized. Audio call applications are required and can be used in this category.
- Avaudiosessioncategoryaudioprocessing, only local audio codec processing is supported. Playback and recording are not supported.
- Avaudiosessioncategorymultiroute, supports audio playback and recording. Allows simultaneous input and output of multiple audio streams. (such as USB connection external speaker output audio, Bluetooth headset simultaneously play another audio this special need)
We can also read the category supported by the current device through the properties of the Avaudiosession
@property(readonly) NSArray<NSString *> *availableCategories;
This ensures device compatibility.
A code example that sets the category is shown below
NSError *setCategoryError = nil;BOOL isSuccess = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];if (!success) { //这里可以读取setCategoryError.localizedDescription查看错误原因}
3. Avaudiosession Mode&&options
The category you just introduced defines seven main scenarios, and the actual development requirements sometimes require a fine tuning of the category, and we find that the interface has two parameter mode and options.
/* Set session category and mode with options */-(bool) Setcategory:(NSString *) category mode:(NSString *) mode Span class= "Hljs-selector-tag" >options:(avaudiosessioncategoryoptions) Options error:(nserror * *) outerror api_available (ios ( 10.0), watchos (3 .0), tvos (10 .0))
Avaudiosession Mode
We get the mode supported by the current device by reading this property
@property(readonly) NSArray<NSString *> *availableModes;
Seven types of mode are available under iOS to customize our category behavior
Mode |
compatible Category |
Scene |
Avaudiosessionmodedefault |
All |
Default mode |
Avaudiosessionmodevoicechat |
Avaudiosessioncategoryplayandrecord |
Voip |
Avaudiosessionmodegamechat |
Avaudiosessioncategoryplayandrecord |
Game recording, Gkvoicechat Auto set |
Avaudiosessionmodevideorecording |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryrecord |
Recording video |
Avaudiosessionmodemovieplayback |
Avaudiosessioncategoryplayback |
Video playback |
Avaudiosessionmodemeasurement |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryrecord Avaudiosessioncategoryplayback |
Minimum system |
Avaudiosessionmodevideochat |
Avaudiosessioncategoryplayandrecord |
Video Call |
Each mode is described below
Avaudiosessionmodedefault, default mode, compatible with all Category
Avaudiosessionmodevoicechat, suitable for VoIP type applications. Can only be under the Avaudiosessioncategoryplayandrecord category. In this mode the system will automatically configure Avaudiosessioncategoryoptionallowbluetooth this option. The system automatically selects the best built-in microphone combination to support voice chat.
Avaudiosessionmodevideochat, for video chat type application, can only be under the Avaudiosessioncategoryplayandrecord category. The system automatically configures the Avaudiosessioncategoryoptionallowbluetooth and Avaudiosessioncategoryoptiondefaulttospeaker options for this mode. The system automatically selects the best built-in microphone combination to support video chat.
Avaudiosessionmodegamechat, suitable for gaming applications. Apps that use the Gkvoicechat object will automatically set this mode and Avaudiosessioncategoryplayandrecord Category. The actual parameters are consistent with the Avaudiosessionmodevideochat
Avaudiosessionmodevideorecording for applications that use the camera to capture video. can only be avaudiosessioncategoryplayandrecord and Avaudiosessioncategoryrecord under the two category. This mode is combined with the avcapturesession API to allow for better control of the audio and video input and output paths. (for example, to set the Automaticallyconfiguresapplicationaudiosession property, the system automatically chooses the best output path.)
Avaudiosessionmodemeasurement, minimizing the system. Only for Avaudiosessioncategoryplayandrecord, Avaudiosessioncategoryrecord, avaudiosessioncategoryplayback these categories.
Avaudiosessionmodemovieplayback for applications that play video. Only for Avaudiosessioncategoryplayback this category.
Avaudiosession Options
We can also use the options to fine-tune the category behavior, such as the following table
Option |
option Feature Description |
compatible Category |
Avaudiosessioncategoryoptionmixwithothers |
Support and other app audio mix |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryplayback Avaudiosessioncategorymultiroute |
Avaudiosessioncategoryoptionduckothers |
System Smart lower other app audio volume |
Avaudiosessioncategoryplayandrecord Avaudiosessioncategoryplayback Avaudiosessioncategorymultiroute |
Avaudiosessioncategoryoptionallowbluetooth |
supports Bluetooth audio input |
Avaudiosessioncategoryrecord Avaudiosessioncategoryplayandrecord |
Avaudiosessioncategoryoptiondefaulttospeaker |
Set default output audio to Speaker |
Avaudiosessioncategoryplayandrecord |
Tuning our Category
With the category and the right mode and options we can tune our results, and here are two scenarios:
used the high-level map of all know, in the background to play QQ music, if the navigation voice out, QQ music will not stop, but is intelligent low and mix, and so on after the navigation voice broadcast, QQ music normal play, here we need to play music backstage, So the category uses Avaudiosessioncategoryplayback, which requires mixing and intelligence to lower the volume of other apps, so options Avaudiosessioncategoryoptionmixwithothers and Avaudiosessioncategoryoptionduckothers
Code examples such as the following
BOOL isSuccess = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers error:&setCategoryError];
Or I want to avaudiosessioncategoryplayandrecord this category the default audio is played by the speaker, so you can call this interface to adjust the category
- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError
By choosing the right and Category,mode and options, you can tune the input and output of your audio to meet your daily development needs (note that category,mode,option is used in conjunction rather than a simple combination, That is, some type of category supports some mode and option, as can be seen from the table above)
4. Audio Interrupt Processing
Other apps or phones will interrupt our app audio, so we have to deal with them accordingly.
We can get the audio interrupt event by listening to the Avaudiosessioninterruptionnotification key.
Callback back UserInfo has a key value
- Avaudiosessioninterruptiontypekey:
The value Avaudiosessioninterruptiontypebegan indicates the start of the interrupt
Value avaudiosessioninterruptiontypeended indicates end of break
Interrupt start: What we need to do is to save the playback status, context, update user interface, etc.
End of break: all we have to do is restore the status and context, update the user interface, and choose whether or not to activate our session when it's ready.
Choose a different audio playback technology, processing interrupt mode also has a difference, as follows:
System sound Services: Broadcast audio using system voice services, which is automatically processed and not controlled by the app, when an interrupt occurs, audio playback is muted and audio playback resumes when the interrupt is complete.
The AV Foundation Framework:avaudioplayer class and the Avaudiorecorder class provide an interrupt start and end Delegate callback method to handle interrupts. Interrupt occurs, the system will automatically stop playback, need to do is record playback time and other status, update the user interface, and so on after the end of the interrupt, the system will automatically activate the session.
Audio Queue Services, I/O Audio unit: Using the Aduio unit these technologies need to handle interrupts, need to be recorded to play or record the location of the interruption after the end of their own audio session.
OpenAL: When playing with OpenAL, you also need to listen for interrupts yourself. Manages the openal context and resumes the audio session after the user has finished interrupting.
It should be noted that: 1. There is an interrupt start event, not necessarily the end of the interruption of the event, so the user needs to enter the foreground, click the UI operation, need to save the playback state and the audio session management, so as not to affect the app's voice function. 2. Audio resources on the competition, it must be telephone priority. 3. Avaudiosession can also monitor peripheral audio status, such as the headset unplugged. Not here to do the statement
5. Avaudiosession Summary
Avaudiosession's role is to manage the allocation of audio, the only hardware resource, by tuning the appropriate avaudiosession to match the functional needs of our app for audio. When switching the audio scene, the corresponding switch avaudiosession is required.
Literature: Audio Session Programming Guide
from:https://www.jianshu.com/p/fb0e5fb71b3c
"Go" IOS audio-avaudiosession