harmony 鸿蒙Volume Management

  • 2023-06-24
  • 浏览 (670)

Volume Management

You can use different APIs to manage the system volume and audio stream volume. The system volume and audio stream volume refer to the volume of a OpenHarmony device and the volume of a specified audio stream, respectively. The audio stream volume is restricted by the system volume.

System Volume

The API for managing the system volume is AudioVolumeManager. Before using this API, you must call getVolumeManager() to obtain an AudioVolumeManager instance. Currently, this API can be used to obtain volume information and listen for volume changes. It cannot be used to adjust the system volume.

import audio from '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';

let audioManager = audio.getAudioManager();
let audioVolumeManager = audioManager.getVolumeManager();

Listening for System Volume Changes

You can set an event to listen for system volume changes.

audioVolumeManager.on('volumeChange', (volumeEvent: audio.VolumeEvent) => {
  console.info(`VolumeType of stream: ${volumeEvent.volumeType} `);
  console.info(`Volume level: ${volumeEvent.volume} `);
  console.info(`Whether to updateUI: ${volumeEvent.updateUi} `);
});

Adjusting the System Volume (for System Applications Only)

Currently, the system volume is mainly adjusted by using system APIs, which serve 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.

Audio Stream Volume

The API for managing the audio stream volume is setVolume() in the AVPlayer or AudioRenderer class. The code snippet below is used for setting the audio stream volume by using the AVPlayer class:

let volume = 1.0 // Specified volume. The value range is [0.00-1.00]. The value 1 indicates the maximum volume.
avPlayer.setVolume(volume);

The code snippet below is used for setting the audio stream volume by using the AudioRenderer class:

audioRenderer.setVolume(0.5).then(() => {  // The volume range is [0.0-1.0].
  console.info('Invoke setVolume succeeded.');
}).catch((err: BusinessError) => {  
  console.error(`Invoke setVolume failed, code is ${err.code}, message is ${err.message}`);
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Development

harmony 鸿蒙Audio Decoding

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Audio Encoding

harmony 鸿蒙Audio Input Device Management

harmony 鸿蒙Audio Output Device Management

harmony 鸿蒙Audio Playback Concurrency Policy

harmony 鸿蒙Audio Playback Development

0  赞