As a hardware resource, audio output is unique to IOS system. It is necessary to control each app through the system level global object “avaudiosession”.
The control distance is as follows:
7GmDJTSBWag18Ae.png
Common audio scenes
- Is it recording or playing?
- How to behave when the system mute key is pressed?
- Is the sound played from the speaker or from the earpiece?
- How to perform after plugging in and out the headset?
- How do you behave when the incoming call / alarm clock rings?
- How do other audio apps behave after starting?
What can be done through “avaudiosession”
- Select the route of input and output for app:
Play through speaker or earpiece – can be set manually
Play through the speaker / earpiece or headset on the mobile phone – the system automatically switches and inserts the default headset into the headset - Select the route entered for the app:
Collect through the mobile phone microphone or headset microphone – the system automatically switches and inserts the default headset into the headset - Coordinate the association between audio playing apps and the sound processing of the system
- Handle the situation after being interrupted by other apps
- Record or play music
Avaudiosession default behavior
- You can play, but you cannot record.
- When the user turns the mute dial on the mobile phone to the “mute” state, if the audio is playing, the playing content will be muted.
- When the user presses the lock key of the mobile phone or the mobile phone locks the screen automatically, if the audio is playing at this time, the playback will be muted and suspended.
- If QQ music or other apps are playing when your app starts playing, other players will be muted and suspended.
The system will activate the globally unique avaudiosession when the app starts
Seven scenes of audio mode
The scene is controlled by seven categories:
Session type | explain | Cause app interruption | Recording and playback | Whether to mute when pressing “mute” or locking the screen |
---|---|---|---|---|
AVAudioSessionCategoryAmbient | Mix playback, which can be played simultaneously with other audio applications | no | Play only | yes |
AVAudioSessionCategorySoloAmbient | Exclusive play | yes | Play only | yes |
AVAudioSessionCategoryPlayback | Background playback, exclusive | yes | Play only | no |
AVAudioSessionCategoryRecord | Recording mode | yes | Recording only | no |
AVAudioSessionCategoryPlayAndRecord | Play and record. At this time, you can record or play | no | Recording and playing | no |
AVAudioSession CategoryAudioProcessing | The hardware decodes the audio and cannot play and record at this time | – | – | – |
AVAudioSessionCategoryMultiRoute | Multiple inputs and outputs, such as headphones and USB devices, can be played at the same time | yes | Recording and playing | no |
Default: categorysoloambient (exclusive playback)
- Avaudiosessioncategoryambient: it is only used when playing music and can be played simultaneously with QQ music. For example, if you want to listen to songs of QQ music when playing games, set the background sound of the game to this category. At the same time, when the user locks the screen or mutes, it will also be muted. This category is basically applicable to the background scenes of all apps.
- Avaudiosessioncategorysoloambient: it is also only used for playback, but different from “avaudiosessioncategoryamambient”, you can’t listen to QQ music with it. For example, apps that don’t want QQ music interference are similar to rhythm masters. Similarly, when the user locks the screen or mutes, it will also be muted, and the rhythm master cannot be played when the screen is locked.
- Avaudiosessioncategoryplayback: what if the screen is locked and you still want to hear the sound? Using this category, for example, app itself is a player. At the same time, when app plays, other similar QQ music cannot be played. Therefore, this category is generally used for player app
- Avaudiosessioncategoryrecord: if you have a player, you must have a recorder. For example, this category is used for wechat voice recording. Since you want quiet recording, you certainly don’t want QQ music, so other playing sounds will be interrupted. Think about the scene of wechat voice, and you’ll know when to use it.
- Avaudiosessioncategoryplayandrecord: what mode should I use if I want to play and record? For example, for VoIP and phone calls, playandrecord is specially designed for such scenes.
- Avaudiosessioncategorymultiroute: imagine an app for DJ. The mobile phone is connected with HDMI to the speaker to play the current music, and then the next song is played in the headset. This kind of scene is not understood by ordinary people. This category can support multiple device input and output.
- Avaudiosessioncategoryaudioprocessing: mainly used for audio format processing. It can generally be used in conjunction with audiounit
The seven categories described above can be regarded as setting seven main scenarios, and these seven categories can certainly not meet all the needs of developers. The method provided by avaudiosession is to first set a tone in seven categories, and then fine tune it. Avaudiosession provides some options for fine tuning each category.
Options for categories
option | Applicable category | effect |
---|---|---|
AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, AVAudioSessionCategoryMultiRoute | Mix with other background apps |
AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, AVAudioSessionCategoryMultiRoute | Lower other app sounds |
AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryRecord, AVAudioSessionCategoryPlayAndRecord | Support Bluetooth headset |
AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryPlayAndRecord | Default handsfree sound |
- Avaudiosessioncategoryoptionmixwithothers: if it is a practical background sound implemented by avaudiosessioncategoryplayback, but you want to coexist with QQ music, you can set this option under the category of avaudiosessioncategoryplayback to achieve common sound.
- Avaudiosessioncategoryoptionduck others: in the scene of real-time call, such as QQ music, when making a video call, you will find that the automatic sound of QQ music decreases. At this time, you can set this option to suppress other music apps.
- Avaudiosessioncategoryoptionallowbluetooth: if you want to support Bluetooth headset phone, you need to set this option
- Avaudiosessioncategoryoptiondefaulttospeaker: if you want to turn on the hands-free function by default in VoIP mode, you need to set this option
Seven modes
pattern | Applicable categories | scene |
---|---|---|
AVAudioSessionModeDefault | All categories | Default mode |
AVAudioSessionModeVoiceChat | AVAudioSessionCategoryPlayAndRecord | VoIP |
AVAudioSessionModeGameChat | AVAudioSessionCategoryPlayAndRecord | Game recording is automatically set by gkvoicechat without manual call |
AVAudioSessionModeVideoRecording | AVAudioSessionCategoryPlayAndRecord AVAudioSessionCategoryRecord | When recording video |
AVAudioSessionModeMoviePlayback | AVAudioSessionCategoryPlayback | Video playback |
AVAudioSessionModeMeasurement | AVAudioSessionCategoryPlayAndRecord AVAudioSessionCategoryRecord AVAudioSessionCategoryPlayback | Minimum system |
AVAudioSessionModeVideoChat | AVAudioSessionCategoryPlayAndRecord | Video call |
- Avaudiosessionmodedefault: This is the default mode for each category.
- Avaudiosessionmodevoicechat: it is mainly used in VoIP scenarios. At this time, the system will select the best input device. For example, when the headset is plugged in, the microphone on the headset will be used for acquisition. At the same time, the category option will be set to “avaudiosessioncategoryoptionallowbluetooth”, so as to support Bluetooth headset.
- Avaudiosessionmodevideochat: mainly used for video calls, such as QQ video and FaceTime. The system will select the best input device. For example, when the headset is plugged in, the microphone on the headset will be used for acquisition, and the category options will be set to “avaudiosessioncategoryoptionallowbluetooth” and “avaudiosessioncategoryoptiondefaulttospeaker”.
- Avaudiosessionmodegamechat: applicable to the collection and playback of game apps, such as “gkvoicechat” objects, which generally do not need to be set manually
Each mode has its applicable category, which can be combined according to specific needs.
Audio notification
System interrupt notification
If the app is making a video call, suddenly a call comes and the alarm clock rings, the most common scenario is to pause first and continue when it is restored. App can process by listening to the system message: avaudiosessioninterruptionnotification.
The system message userinfo mainly contains two keys:
- Avaudiosessioninterruptiontypekey: the value avaudiosessioninterruptiontypebegan indicates that the interruption starts, and we should pause playback and collection. The value avaudiosessioninterruptiontypeended indicates that the interruption ends, and we can continue playback and collection.
- Avaudiosessioninterruptionoptionkey: Currently, there is only one value. Avaudiosessioninterruptionoptionshouldresume indicates that playback and collection should also be resumed at this time.
Other app occupancy notification
When other apps occupy audiosession, use avaudiosessionsilencesecondaryaudiohintnotification to notify. The userinfo key returned from the callback is avaudiosessionsilencesecondaryaudiohinttypekey
- Typeaudiosessionsecond: audiosessionsecond
- Avaudiosessionsilencesecondaryaudiohinttypeend: indicates that other apps begin to release sessions
Peripheral change notification
By default, avaudiosession will select an optimal output scheme when the app is started. For example, use headphones when inserting headphones. However, in this process, the user may pull out the headset, and the app can handle it by listening to the system message: avaudiosessionroutechangenotification.
Settings for video call scenarios
Category: avaudiosessioncategoryplayandrecord
Options for categories:
AVAudioSessionCategoryOptionAllowBluetooth
AVAudioSessionCategoryOptionDefaultToSpeaker
Mode: avaudiosessionmodevideochat
If you want to record an audio and then play it.
It is suggested to modify the recording to:
Avaudiosessioncategoryrecord or
AVAudioSessionCategoryPlayAndRecord
That is, it supports recording mode
image.png
It is suggested to modify the playback to:
Avaudiosessioncategorysoloambient or
Avaudiosessioncategoryambient or
AVAudioSessionCategoryPlayback
That is, play only mode.
image.pngNote: after my test, it is used when playing
AVAudioSessionCategoryPlayAndRecord
(recording and playing mode), which will cause the playing sound to be very small. Available at this timeAVAudioSessionCategorySoloAmbient
orAVAudioSessionCategoryAmbient
orAVAudioSessionCategoryPlayback
To solve the problem of low sound.
Reference article:
https://www.cnblogs.com/junhuawang/p/7920989.html
https://www.jianshu.com/p/3e0a399380df/