Audio Effect Management (ArkTS)

You can manage the audio effect of a specific playback instance, for example, obtaining or setting the audio effect mode of the current audio playback stream. You can obtain the global audio effect, that is, the audio effect mode corresponding to a specific audio stream usage, which is specified by StreamUsage.

Managing the Audio Effect of a Playback Instance

You can call getAudioEffectMode() and setAudioEffectMode(mode: AudioEffectMode) to obtain and set the audio effect mode of the current audio playback stream. The audio effect mode can be disabled (EFFECT_NONE) or default (EFFECT_DEFAULT). In the default audio effect mode, the audio effect of the corresponding scenario is automatically loaded based on StreamUsage of the audio stream.

Creating a Playback Instance

Before the management, you must call createAudioRenderer(options: AudioRendererOptions) to create an AudioRenderer instance.

  1. Import the audio module.

    import audio from '@ohos.multimedia.audio';
    
  2. Configure audio rendering parameters and create an AudioRenderer instance. For details about the audio rendering parameters, see AudioRendererOptions. For the AudioRenderer instance, the audio effect mode EFFECT_DEFAULT is used by default.

    import { BusinessError } from '@ohos.base';
    
    let audioStreamInfo: audio.AudioStreamInfo = {
      samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
      channels: audio.AudioChannel.CHANNEL_1,
      sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
      encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
    };
    
    let audioRendererInfo: audio.AudioRendererInfo = {
      usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
      rendererFlags: 0
    };
    
    let audioRendererOptions: audio.AudioRendererOptions = {
      streamInfo: audioStreamInfo,
      rendererInfo: audioRendererInfo
    };
    let audioRenderer: audio.AudioRenderer | undefined = undefined;
    
    audio.createAudioRenderer(audioRendererOptions, (err: BusinessError, data: audio.AudioRenderer) => {
      if (err) {
        console.error(`Invoke createAudioRenderer failed, code is ${err.code}, message is ${err.message}`);
        return;
      } else {
        console.info('Invoke createAudioRenderer succeeded.');
        audioRenderer = data;
      }
    });
    

Obtaining the Audio Effect Mode of the Playback Instance

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

audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => {
  if (err) {
    console.error(`Failed to get params, code is ${err.code}, message is ${err.message}`);
    return;    
  } else {
    console.info(`getAudioEffectMode: ${effectMode}`);
  }
});

Setting an Audio Effect Mode for the Playback Instance

Disable the system audio effect.

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

audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_NONE, (err: BusinessError) => {
  if (err) {
    console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
    return;
  } else {
    console.info('Callback invoked to indicate a successful audio effect mode setting.');
  }
});

Enable the default system audio effect.

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

audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => {
  if (err) {
    console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
    return;
  } else {
    console.info('Callback invoked to indicate a successful audio effect mode setting.');
  }
});

Obtaining the Global Audio Effect Mode

Obtain the global audio effect mode corresponding to a specific audio stream usage (specified by StreamUsage).

For an audio playback application, pay attention to the audio effect mode used by the audio stream of the application and perform corresponding operations. For example, for a music application, select the audio effect mode for the music scenario. Before obtaining the global audio effect mode, call getStreamManager() to create an AudioStreamManager instance.

Creating an AudioStreamManager Instance

Before using AudioStreamManager APIs, you must use getStreamManager() to create an AudioStreamManager instance.

import audio from '@ohos.multimedia.audio';

let audioManager = audio.getAudioManager();
let audioStreamManager = audioManager.getStreamManager();

Querying the Audio Effect Mode of the Corresponding Scenario

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

audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MEDIA, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => {
  if (err) {
    console.error('Failed to get effect info array');
    return;    
  } else {
    console.info(`getAudioEffectInfoArray: ${audioEffectInfoArray}`);
  }
});