1e41f4b71Sopenharmony_ci# Extended Audio Concurrency Strategy (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe audio system offers a default concurrency strategy based on the audio stream type. If the default concurrency strategy cannot meet your service requirements, you can use an audio session to customize a strategy. The audio session is an extension of the default audio concurrency strategy. Example scenarios are as follows: 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- When an application that continuously plays a short sound does not want to be paused when running in the background, it can obtain an audio session to ensure a seamless integration of the entire playback process. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- If the default concurrency strategy does not meet service requirements, an application can request an audio session and specify a strategy. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe following figure shows the audio session process. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Prerequisites 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciTo use the audio session management capability provided by OHAudio, add the corresponding header file. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### Linking the Dynamic Link Library in the CMake Script 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci``` cmake 20e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohaudio.so) 21e41f4b71Sopenharmony_ci``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci### Adding Header Files 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciInclude the **native_audio_session_manager.h** header file so that the application can use the functions related to audio playback. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```cpp 28e41f4b71Sopenharmony_ci#include <ohaudio/native_audio_session_manager.h> 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## Audio Session Strategy 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciIn scenarios where multiple audio streams are active, an application can proactively establish an audio session strategy to manage how other applications handle audio streams once it has acquired audio focus. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### Audio Concurrency Mode 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciWhen the default concurrency strategy cannot meet service requirements, you can configure the audio concurrency mode. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciFour audio concurrency modes are provided. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- **CONCURRENCY_DEFAULT**: default concurrency strategy, which is used no audio session is used. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- **CONCURRENCY_MIX_WITH_OTHERS**: mixes audio streams with other applications that are concurrently playing audio. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci It is assumed that application A adopts **CONCURRENCY_MIX_WITH_OTHERS** and is playing audio streams, and application B applies to play audio streams at a later time. The relationship between applications A and B is described in the table below. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci | Application Behavior Before Mode Configuration| Application Behavior After Mode Configuration| 48e41f4b71Sopenharmony_ci | ------------ | ------------------ | 49e41f4b71Sopenharmony_ci | Application A rejects the playback request of application B.| Application A mixes its audio playback with that of application B.| 50e41f4b71Sopenharmony_ci | Application A pauses the playback of application B.| Application A mixes its audio playback with that of application B.| 51e41f4b71Sopenharmony_ci | Application A lowers the volume of application B.| Application A mixes its audio playback with that of application B.| 52e41f4b71Sopenharmony_ci | Application B lowers the volume of application A.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 53e41f4b71Sopenharmony_ci | Application B pauses the playback of application A.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 54e41f4b71Sopenharmony_ci | Application B stops the playback of application A.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci It is assumed that application A is playing audio streams, and application B adopts **CONCURRENCY_MIX_WITH_OTHERS** and applies to play audio streams at a later time. The relationship between applications A and B is described in the table below. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci | Application Behavior Before Mode Configuration| Application Behavior After Mode Configuration| 59e41f4b71Sopenharmony_ci | ------------ | ------------------ | 60e41f4b71Sopenharmony_ci | Application A rejects the playback request of application B.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 61e41f4b71Sopenharmony_ci | Application A pauses the playback of application B.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 62e41f4b71Sopenharmony_ci | Application A lowers the volume of application B.| If applications and B have similar types, application A mixes its audio playback with that of application B.| 63e41f4b71Sopenharmony_ci | Application B lowers the volume of application A.| Application A mixes its audio playback with that of application B.| 64e41f4b71Sopenharmony_ci | Application B pauses the playback of application A.| Application A mixes its audio playback with that of application B.| 65e41f4b71Sopenharmony_ci | Application B stops the playback of application A.| Application A mixes its audio playback with that of application B.| 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci- **CONCURRENCY_DUCK_OTHERS**: lowers the volume of the application that is currently playing the audio. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci It is assumed that application A is playing audio streams, and application B adopts **CONCURRENCY_DUCK_OTHERS** and applies to play audio streams at a later time. The relationship between applications A and B is described in the table below. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci | Application Behavior Before Mode Configuration| Application Behavior After Mode Configuration| 72e41f4b71Sopenharmony_ci | ------------ | ------------------ | 73e41f4b71Sopenharmony_ci | Application B pauses the playback of application A.| Application B lowers the volume of application A.| 74e41f4b71Sopenharmony_ci | Application B stops the playback of application A.| Application B lowers the volume of application A.| 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci- **CONCURRENCY_PAUSE_OTHERS**: pauses the application that is currently playing the audio. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci It is assumed that application A is playing audio streams, and application B that adopts **CONCURRENCY_PAUSE_OTHERS** applies to play audio streams at a later time. The relationship between applications A and B is described in the table below. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci | Application Behavior Before Mode Configuration| Application Behavior After Mode Configuration| 81e41f4b71Sopenharmony_ci | ------------ | ------------------ | 82e41f4b71Sopenharmony_ci | Application B stops the playback of application A.| Application B pauses the playback of application A.| 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci## Audio Session Deactivation Event 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ciIt is recommended that an application listen for audio session deactivation events. When such an event occurs, the system performs corresponding operations on the related audio stream based on the reason of the deactivation. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciIf the application wants to customize a concurrency strategy through the audio session again, it must activate the audio session first. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci### Reasons for Audio Session Deactivation 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciTwo audio session deactivation reasons are preset: 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci- **DEACTIVATED_LOWER_PRIORITY**: The focus of the application that is playing audio streams is forcibly cleared. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci- **DEACTIVATED_TIME_OUT**: The default duration is reached after the application creates an audio session or stops audio playback. In this case, the volume of the application that was lowered in volume by this session will be restored to its original level, and the application that was paused by this session will receive a stop playback focus event. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci## Obtaining an Audio Session Manager 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciCreate an **OH_AudioSessionManager** instance. Before using audio session management, you must call **OH_AudioManager_GetAudioSessionManager** to create an **OH_AudioSessionManager** instance. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci ```cpp 103e41f4b71Sopenharmony_ci OH_AudioSessionManager *audioSessionManager; 104e41f4b71Sopenharmony_ci OH_AudioManager_GetAudioSessionManager(&audioSessionManager); 105e41f4b71Sopenharmony_ci ``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci## Activating an Audio Session 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciCall **OH_AudioSessionManager_ActivateAudioSession** to activate an audio session. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci ```cpp 112e41f4b71Sopenharmony_ci OH_AudioSession_Strategy strategy = {CONCURRENCY_MIX_WITH_OTHERS}; 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci OH_AudioSessionManager_ActivateAudioSession(audioSessionManager, &strategy); 115e41f4b71Sopenharmony_ci ``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci## Deactivating an Audio Session 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ciCall **OH_AudioSessionManager_DeactivateAudioSession** to deactivate an audio session when it is no longer needed. 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci ```cpp 122e41f4b71Sopenharmony_ci OH_AudioSessionManager_DeactivateAudioSession(audioSessionManager); 123e41f4b71Sopenharmony_ci ``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci## Checking Whether an Audio Session Is Activated 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciCall **OH_AudioSessionManager_IsAudioSessionActivated** to check whether an audio session is activated. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci ```cpp 130e41f4b71Sopenharmony_ci bool isActivated = OH_AudioSessionManager_IsAudioSessionActivated(audioSessionManager); 131e41f4b71Sopenharmony_ci ``` 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci## Registering and Unregistering the Audio Session Deactivation Event Callback 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci### Defining a Callback 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci ```cpp 138e41f4b71Sopenharmony_ci int32_t MyAudioSessionDeactivatedCallback(OH_AudioSession_DeactivatedEvent event) 139e41f4b71Sopenharmony_ci { 140e41f4b71Sopenharmony_ci switch(event.reason) { 141e41f4b71Sopenharmony_ci case DEACTIVATED_LOWER_PRIORITY: 142e41f4b71Sopenharmony_ci // The application focus is preempted. 143e41f4b71Sopenharmony_ci return 0; 144e41f4b71Sopenharmony_ci case DEACTIVATED_TIMEOUT: 145e41f4b71Sopenharmony_ci // A timeout error occurs. 146e41f4b71Sopenharmony_ci return 0; 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci ``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci### Registering a Callback to Listen for Audio Session Deactivation Events 152e41f4b71Sopenharmony_ciCall **OH_AudioSessionManager_RegisterSessionDeactivatedCallback** to register a callback to listen for audio session deactivation events. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci ```cpp 155e41f4b71Sopenharmony_ci OH_AudioSessionManager_RegisterSessionDeactivatedCallback(audioSessionManager, MyAudioSessionDeactivatedCallback); 156e41f4b71Sopenharmony_ci ``` 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci### Unregistering the Callback Used to Listen for Audio Session Deactivation Events 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ciCall **OH_AudioSessionManager_UnregisterSessionDeactivatedCallback** to unregister the callback used to listen for audio session deactivation events. 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci ```cpp 163e41f4b71Sopenharmony_ci OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(audioSessionManager, MyAudioSessionDeactivatedCallback); 164e41f4b71Sopenharmony_ci ``` 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci## Sample Code 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciRefer to the sample code below to complete the process of creating, activating, and listening of an audio session. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci ```cpp 171e41f4b71Sopenharmony_ci #include <cstdint> 172e41f4b71Sopenharmony_ci #include "ohaudio/native_audio_session_manager.h" 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci int32_t MyAudioSessionDeactivatedCallback(OH_AudioSession_DeactivatedEvent event) 175e41f4b71Sopenharmony_ci { 176e41f4b71Sopenharmony_ci switch(event.reason) { 177e41f4b71Sopenharmony_ci case DEACTIVATED_LOWER_PRIORITY: 178e41f4b71Sopenharmony_ci // The application focus is preempted. 179e41f4b71Sopenharmony_ci return 0; 180e41f4b71Sopenharmony_ci case DEACTIVATED_TIMEOUT: 181e41f4b71Sopenharmony_ci // A timeout error occurs. 182e41f4b71Sopenharmony_ci return 0; 183e41f4b71Sopenharmony_ci } 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci OH_AudioSessionManager *audioSessionManager; 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci // Create an OH_AudioSessionManager instance. 189e41f4b71Sopenharmony_ci OH_AudioCommon_Result resultManager = OH_AudioManager_GetAudioSessionManager(&audioSessionManager); 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci OH_AudioSession_Strategy strategy = {CONCURRENCY_MIX_WITH_OTHERS}; 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci // Set an audio concurrency mode and activate an audio session. 194e41f4b71Sopenharmony_ci OH_AudioCommon_Result resultActivate = OH_AudioSessionManager_ActivateAudioSession(audioSessionManager, &strategy); 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci // Check whether the audio session is activated. 197e41f4b71Sopenharmony_ci bool isActivated = OH_AudioSessionManager_IsAudioSessionActivated(audioSessionManager); 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci // Listen for audio session deactivation events. 200e41f4b71Sopenharmony_ci OH_AudioCommon_Result resultRegister = OH_AudioSessionManager_RegisterSessionDeactivatedCallback(audioSessionManager, MyAudioSessionDeactivatedCallback); 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci // After the audio session is activated, the application can perform operations such as playing, pausing, stopping, and releasing audio streams. 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci // Cancel listening for audio session deactivation events. 205e41f4b71Sopenharmony_ci OH_AudioCommon_Result resultUnregister = OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(audioSessionManager, MyAudioSessionDeactivatedCallback); 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci // Deactivate the audio session. 208e41f4b71Sopenharmony_ci OH_AudioCommon_Result resultDeactivate = OH_AudioSessionManager_DeactivateAudioSession(audioSessionManager); 209e41f4b71Sopenharmony_ci ``` 210