1# Using AudioSession to Manage Audio Focus (ArkTS) 2 3In the scenario where multiple audio streams are concurrently playing, the system has preset a [default audio focus strategy](audio-playback-concurrency.md#audio-focus-strategy) for unified audio focus management of all streams (including playback and recording). 4 5An application can use an audio session provided by the audio session manager to actively manage the audio focus. Specifically, it can customize an audio focus strategy and determine the timing for releasing the audio focus, thereby meeting its specific service needs. 6 7This topic describes the usage and precautions of the ArkTS APIs related to the audio session. For more information about the audio focus and audio session, see [Introduction to Audio Focus and Audio Session](audio-playback-concurrency.md). 8 9## Obtaining an Audio Session Manager 10 11Create an **AudioSessionManager** instance. Before using any APIs of **AudioSessionManager**, you must call [getSessionManager](../../reference/apis-audio-kit/js-apis-audio.md#getsessionmanager12) to obtain an **AudioSessionManager** instance. 12 13 ```ts 14 import { audio } from '@kit.AudioKit'; 15 16 let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager(); 17 ``` 18 19## Activating an Audio Session 20 21Call [AudioSessionManager.activateAudioSession](../../reference/apis-audio-kit/js-apis-audio.md#activateaudiosession12) to activate an audio session. 22 23During the activation, specify an [audio session strategy](audio-playback-concurrency.md#audio-session-strategy). The strategy contains the **concurrencyMode** parameter, which is of the [AudioConcurrencyMode](../../reference/apis-audio-kit/js-apis-audio.md#audioconcurrencymode12) type and is used to declare the audio concurrency strategy. 24 25 ```ts 26 import { audio } from '@kit.AudioKit'; 27 import { BusinessError } from '@kit.BasicServicesKit'; 28 29 let strategy: audio.AudioSessionStrategy = { 30 concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS 31 }; 32 33 audioSessionManager.activateAudioSession(strategy).then(() => { 34 console.info('activateAudioSession SUCCESS'); 35 }).catch((err: BusinessError) => { 36 console.error(`ERROR: ${err}`); 37 }); 38 ``` 39 40## Checking Whether an Audio Session Is Activated 41 42Call [isAudioSessionActivated](../../reference/apis-audio-kit/js-apis-audio.md#isaudiosessionactivated12) to check whether an audio session is activated. 43 44 ```ts 45 let isActivated = audioSessionManager.isAudioSessionActivated(); 46 ``` 47 48## Deactivating an Audio Session 49 50Call [deactivateAudioSession](../../reference/apis-audio-kit/js-apis-audio.md#deactivateaudiosession12) to deactivate an audio session. 51 52 ```ts 53 import { BusinessError } from '@kit.BasicServicesKit'; 54 55 audioSessionManager.deactivateAudioSession().then(() => { 56 console.info('deactivateAudioSession SUCCESS'); 57 }).catch((err: BusinessError) => { 58 console.error(`ERROR: ${err}`); 59 }); 60 ``` 61 62## Listening for Audio Session Deactivation Events 63 64Call [on('audioSessionDeactivated')](../../reference/apis-audio-kit/js-apis-audio.md#onaudiosessiondeactivated12) to listen for the [AudioSessionDeactivatedEvent](../../reference/apis-audio-kit/js-apis-audio.md#audiosessiondeactivatedevent12) event. 65 66When an audio session is deactivated (not proactively), the application receives [AudioSessionDeactivatedEvent](../../reference/apis-audio-kit/js-apis-audio.md#audiosessiondeactivatedevent12), which contains [AudioSessionDeactivatedReason](../../reference/apis-audio-kit/js-apis-audio.md#audiosessiondeactivatedreason12). 67 68Upon this event, the application can perform operations based on service requirements, for example, releasing resources or reactivating the audio session. 69 70 ```ts 71 import { audio } from '@kit.AudioKit'; 72 73 audioSessionManager.on('audioSessionDeactivated', (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 74 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 75 }); 76 ``` 77 78## Canceling Listening for Audio Session Deactivation Events 79 80Call [off('audioSessionDeactivated')](../../reference/apis-audio-kit/js-apis-audio.md#offaudiosessiondeactivated12) to cancel listening for **AudioSessionDeactivatedEvent**. 81 82 ```ts 83 audioSessionManager.off('audioSessionDeactivated'); 84 ``` 85 86## Sample Code 87 88Refer to the sample code below to complete the process of creating, activating, and listening of an audio session. 89 90 ```ts 91 import { audio } from '@kit.AudioKit'; 92 import { BusinessError } from '@kit.BasicServicesKit'; 93 94 let audioManager = audio.getAudioManager(); 95 // Create an AudioSessionManager instance. 96 let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager(); 97 // Set the audio concurrency mode. 98 let strategy: audio.AudioSessionStrategy = { 99 concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS 100 }; 101 102 // Activate an audio session. 103 audioSessionManager.activateAudioSession(strategy).then(() => { 104 console.info('activateAudioSession SUCCESS'); 105 }).catch((err: BusinessError) => { 106 console.error(`ERROR: ${err}`); 107 }); 108 109 // Check whether the audio session is activated. 110 let isActivated = audioSessionManager.isAudioSessionActivated(); 111 112 // Listen for audio session deactivation events. 113 audioSessionManager.on('audioSessionDeactivated', (audioSessionDeactivatedEvent: audio.AudioSessionDeactivatedEvent) => { 114 console.info(`reason of audioSessionDeactivated: ${audioSessionDeactivatedEvent.reason} `); 115 }); 116 117 // After the audio session is activated, the application can perform operations such as playing, pausing, stopping, and releasing audio streams. 118 119 // Deactivate the audio session. 120 audioSessionManager.deactivateAudioSession().then(() => { 121 console.info('deactivateAudioSession SUCCESS'); 122 }).catch((err: BusinessError) => { 123 console.error(`ERROR: ${err}`); 124 }); 125 126 // Cancel listening for audio session deactivation events. 127 audioSessionManager.off('audioSessionDeactivated'); 128 ``` 129