1# Volume Management 2 3This module provides capabilities for managing playback volume, covering system volume, application volume, and audio stream volume. 4 5The system volume is managed globally by OpenHarmony and applies to all applications and devices. OpenHarmony categorizes audio into various stream types, each with its own independent system volume control. 6 7> **NOTE** 8> 9> The system volume can be adjusted using physical volume buttons or through the system settings screen. On the system settings screen, users can individually adjust the volume levels for each type of system volume. 10 11The following lists the common stream types and their corresponding system volumes. 12 13- Media volume: used for media playback such as music, videos, and games. 14- Call volume: used for voice calls. 15- Ringtone volume: used for incoming call ringtones. 16- Alarm volume: used for alarm notifications. 17 18The application volume is a type of volume control provided by OpenHarmony for third-party applications to manage the volume of all audio streams within that application. Once set, all audio streams initiated by the application will use this volume level by default. In addition, applications with the system application permission can adjust the volume of specific applications by using their UIDs. 19 20The audio stream volume is a volume setting controlled independently by an application and affects only the output volume of the specified audio stream within that application. For example, a media player can control its playback volume independently without affecting the system volume or other types of stream volumes within the application. 21 22The following describes the relationship between the system volume, application volume, and audio stream volume. 23 24- Hierarchy: The system volume is global, whereas the application volume and audio stream volume are local. 25 26 The adjustment range for the application volume and audio stream volume is limited by the system volume. For example, if the system media volume is set to 50% and the application volume is set to 100%, the final output volume of the application can only reach 50%. 27 28 The audio stream volume provides more precise control over the application volume. Third-party applications that have set an application volume can further fine-tune the volume of specific audio streams using the audio stream volume. 29- Synergy: The final output volume of an application is determined collectively by the system volume, application volume, and audio stream volume. For example, if the system media volume is set to 50%, the application volume is set to 50%, and the application sets the audio stream volume for media audio to 100%, the final output volume of that audio stream will be 25%. 30 31OpenHarmony achieves precise volume control for applications through the coordinated use of the system volume, app volume, and audio stream volume. 32 33## System Volume 34 35The API for managing system volume is provided by AudioVolumeManager. Before using this API, you must call [getVolumeManager()](../../reference/apis-audio-kit/arkts-apis-audio-AudioManager.md#getvolumemanager9) to obtain an AudioVolumeManager instance. 36 37Currently, AudioVolumeManager can be used to obtain volume information and listen for volume changes. It cannot be used to adjust the system volume. If you want to adjust the system volume, follow the instructions provided in [Adjusting the System Volume Using the Volume Panel](#adjusting-the-system-volume-using-the-volume-panel). 38 39```ts 40import { audio } from '@kit.AudioKit'; 41 42let audioManager = audio.getAudioManager(); 43let audioVolumeManager = audioManager.getVolumeManager(); 44``` 45 46### Obtaining Volume Information 47 48The API for managing system volume is provided by AudioVolumeManager. Before using this API, you must call [getVolumeManager()](../../reference/apis-audio-kit/arkts-apis-audio-AudioManager.md#getvolumemanager9) to obtain an AudioVolumeManager instance. 49 50```ts 51import { audio } from '@kit.AudioKit'; 52 53let audioManager = audio.getAudioManager(); 54let audioVolumeManager = audioManager.getVolumeManager(); 55``` 56 57Call [AudioVolumeManager](../../reference/apis-audio-kit/arkts-apis-audio-AudioVolumeManager.md) to obtain the volume of a specified audio stream. 58 59The example code is as follows: 60 61```ts 62import { audio } from '@kit.AudioKit'; 63import { BusinessError } from '@kit.BasicServicesKit'; 64 65// Obtain the volume of a stream. 66audioVolumeManager.getVolumeByStream(audio.StreamUsage.STREA_USAGE_MUSIC); 67 68// Obtain the minimum volume of a stream. 69audioVolumeManager.getMinVolumeByStream(audio.StreamUsage.STREA_USAGE_MUSIC); 70 71// Obtain the maximum volume of a stream. 72audioVolumeManager.getMaxVolumeByStream(audio.StreamUsage.STREA_USAGE_MUSIC); 73``` 74 75### Listening for System Volume Changes 76 77You can set an event to listen for system volume changes. 78 79```ts 80import { audio } from '@kit.AudioKit'; 81 82audioVolumeManager.on('streamVolumeChange', audio.StreamUsage.STREAM_USAGE_MUSIC, (streamVolumeEvent: audio.StreamVolumeEvent) => { 83 console.info(`StreamUsagem: ${streamVolumeEvent.streamUsage} `); 84 console.info(`Volume level: ${streamVolumeEvent.volume} `); 85 console.info(`Whether to updateUI: ${streamVolumeEvent.updateUi} `); 86}); 87``` 88 89<!--Del--> 90### Adjusting the System Volume (for System Applications Only) 91 92Currently, adjusting the system volume is mainly conducted by using system APIs, which are available for the physical volume button and the Settings application. When the user presses the volume button, a system API is called to adjust the system volume, including the volume for media, ringtone, or notification. 93<!--DelEnd--> 94 95### Adjusting the System Volume Using the Volume Panel 96 97An application cannot directly adjust the system volume. However, it can invoke the system volume panel for users to adjust the volume. When the user adjusts the volume, a volume prompt UI is displayed to explicitly notify the user that the system volume changes. 98 99To achieve this, you can use the ArkTS component **AVVolumePanel** in your application. For details, see the [AVVolumePanel Reference](../../reference/apis-audio-kit/ohos-multimedia-avvolumepanel.md). 100 101## Application Volume 102 103The API for managing application volume is provided by AudioVolumeManager. Before using this API, you must call [getVolumeManager()](../../reference/apis-audio-kit/arkts-apis-audio-AudioManager.md#getvolumemanager9) to obtain an AudioVolumeManager instance. 104 105When [volume mode](../../reference/apis-audio-kit/arkts-apis-audio-e.md#audiovolumemode19) is set to **APP_INDIVIDUAL**, you can set and query the application volume by calling the APIs in the following sample. 106 107### Adjusting the Application Volume 108 109```ts 110import { audio } from '@kit.AudioKit'; 111 112let audioManager = audio.getAudioManager(); 113let audioVolumeManager = audioManager.getVolumeManager(); 114 115// Set the volume (ranging from 0 to 100) for the application. 116audioVolumeManager.setAppVolumePercentage(20).then(() => { 117 console.info(`set app volume success.`); 118}); 119 120// Query the application volume. 121audioVolumeManager.getAppVolumePercentage().then((value: number) => { 122 console.info(`app volume is ${value}.`); 123}); 124 125// Subscribe to the application volume change event. For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 126let appVolumeChangeCallback = (volumeEvent: audio.VolumeEvent) => { 127 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 128 console.info(`Volume level: ${volumeEvent.volume} `); 129 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 130}; 131audioVolumeManager.on('appVolumeChange', appVolumeChangeCallback); 132audioVolumeManager.off('appVolumeChange', appVolumeChangeCallback); 133 134// Cancel all subscriptions to the event. 135audioVolumeManager.off('appVolumeChange'); 136``` 137 138<!--Del--> 139### Adjusting the Application Volume Based on the UID (for System Applications Only) 140 141```ts 142import { audio } from '@kit.AudioKit'; 143 144let uid: number = 20010041; // Application ID. 145let audioManager = audio.getAudioManager(); 146let audioVolumeManager = audioManager.getVolumeManager(); 147 148// Set the volume (ranging from 0 to 100) for an application. 149let volume: number = 20; // Volume to set. 150audioVolumeManager.setAppVolumePercentageForUid(uid, volume).then(() => { 151 console.info(`set app volume success.`); 152}); 153 154// Obtain the volume (ranging from 0 to 100) of an application. 155audioVolumeManager.getAppVolumePercentageForUid(uid).then((value: number) => { 156 console.info(`app volume is ${value}.`); 157}); 158 159// Check whether the application volume is muted. 160audioVolumeManager.isAppVolumeMutedForUid(uid, true).then((value: boolean) => { 161 console.info(`app muted state is ${value}.`); 162}); 163 164// Set the application mute state. 165audioVolumeManager.setAppVolumeMutedForUid(uid, true).then(() => { 166 console.info(`set app mute state success.`); 167}); 168 169// Subscribe to the application volume change event. For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 170let appVolumeChangeForUidCallback = (volumeEvent: audio.VolumeEvent) => { 171 console.info(`VolumeType of stream: ${volumeEvent.volumeType} `); 172 console.info(`Volume level: ${volumeEvent.volume} `); 173 console.info(`Whether to updateUI: ${volumeEvent.updateUi} `); 174}; 175audioVolumeManager.on('appVolumeChangeForUid', uid, appVolumeChangeForUidCallback); 176audioVolumeManager.off('appVolumeChangeForUid', appVolumeChangeForUidCallback); 177 178// Cancel all subscriptions to the event. 179audioVolumeManager.off('appVolumeChangeForUid'); 180``` 181<!--DelEnd--> 182 183## Audio Stream Volume 184 185The **setVolume()** API in both the **AVPlayer** and **AudioRenderer** classes can be used to set the audio stream volume. The code snippet below uses the API in the [AVPlayer](../../reference/apis-media-kit/arkts-apis-media-f.md#mediacreateavplayer9) class: 186 187```ts 188let volume = 1.0; // Specified volume. The value range is [0.00-1.00]. The value 1 indicates the maximum volume. 189avPlayer.setVolume(volume); 190``` 191 192Call [setVolume](../../reference/apis-audio-kit/arkts-apis-audio-AudioRenderer.md#setvolume9) and [getVolume](../../reference/apis-audio-kit/arkts-apis-audio-AudioRenderer.md#getvolume12) of [AudioRenderer](../../reference/apis-audio-kit/arkts-apis-audio-f.md#audiocreateaudiorenderer8) to set and obtain the volume of the audio stream, respectively. 193 194The example code is as follows: 195 196```ts 197import { BusinessError } from '@kit.BasicServicesKit'; 198 199// Set the volume for the audio stream. 200audioRenderer.setVolume(0.5).then(() => { // The volume range is [0.0-1.0]. 201 console.info('Invoke setVolume succeeded.'); 202}).catch((err: BusinessError) => { 203 console.error(`Invoke setVolume failed, code is ${err.code}, message is ${err.message}`); 204}); 205 206// Obtain the volume of the audio stream. 207try { 208 let value: number = audioRenderer.getVolume(); 209 console.info(`Indicate that the volume is obtained ${value}.`); 210} catch (err) { 211 let error = err as BusinessError; 212 console.error(`Failed to obtain the volume, error ${error}.`); 213} 214``` 215 216### Listening for Active Stream Changes 217 218You can set an event to listen for active stream changes. 219 220```ts 221import { audio } from '@kit.AudioKit'; 222 223// Subscribe to the active stream change event. For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 224let activeVolumeTypeChangeCallback = (volumeType: audio.AudioVolumeType) => { 225 console.info(`VolumeType of stream: ${volumeType} `); 226}; 227audioVolumeManager.on('activeVolumeTypeChange', activeVolumeTypeChangeCallback); 228audioVolumeManager.off('activeVolumeTypeChange', activeVolumeTypeChangeCallback); 229 230// Cancel all subscriptions to the event. 231audioVolumeManager.off('activeVolumeTypeChange'); 232``` 233 234<!--no_check-->