• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Effect Management
2
3You 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 also obtain the global audio effect, that is, the audio effect mode corresponding to a specific audio content type (specified by **ContentType**) and audio stream usage (specified by **StreamUsage**).
4
5## Managing the Audio Effect of a Playback Instance
6
7You can obtain and set the audio effect mode, which can be disabled (**EFFECT_NONE**) or default (**EFFECT_DEFAULT**), of the current audio playback stream. In default audio effect mode, the audio effect of the corresponding scenario is automatically loaded based on **ContentType** and **StreamUsage** of the audio stream.
8
9### Creating a Playback Instance
10
11Before obtaining or setting the audio effect mode, you must call **createAudioRenderer(options: AudioRendererOptions)** to create an **AudioRenderer** instance.
12
131. Import the audio module.
14
15  ```ts
16  import audio from '@ohos.multimedia.audio';
17  ```
18
192. Configure audio rendering parameters and create an **AudioRenderer** instance. For details about the audio rendering parameters, see [AudioRendererOptions](../reference/apis/js-apis-audio.md#audiorendereroptions8). For the **AudioRenderer** instance, the audio effect mode **EFFECT_DEFAULT** is used by default.
20
21  ```ts
22  import audio from '@ohos.multimedia.audio';
23  import { BusinessError } from '@ohos.base';
24  let audioStreamInfo: audio.AudioStreamInfo = {
25    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
26    channels: audio.AudioChannel.CHANNEL_1,
27    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
28    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
29  };
30
31  let audioRendererInfo: audio.AudioRendererInfo = {
32    content: audio.ContentType.CONTENT_TYPE_SPEECH,
33    usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
34    rendererFlags: 0
35  };
36
37  let audioRendererOptions: audio.AudioRendererOptions = {
38    streamInfo: audioStreamInfo,
39    rendererInfo: audioRendererInfo
40  };
41
42  audio.createAudioRenderer(audioRendererOptions, (err: BusinessError, data: audio.AudioRenderer) => {
43    if (err) {
44      console.error(`Invoke createAudioRenderer failed, code is ${err.code}, message is ${err.message}`);
45      return;
46    } else {
47      console.info('Invoke createAudioRenderer succeeded.');
48      let audioRenderer: audio.AudioRenderer = data;
49    }
50  });
51  ```
52
53### Obtaining the Audio Effect Mode of the Playback Instance
54
55  ```ts
56  import { BusinessError } from '@ohos.base';
57  audioRenderer.getAudioEffectMode((err: BusinessError, effectmode: audio.AudioEffectMode) => {
58    if (err) {
59      console.error(`Failed to get params, code is ${err.code}, message is ${err.message}`);
60      return;
61    } else {
62      console.info(`getAudioEffectMode: ${effectmode}`);
63    }
64  });
65  ```
66
67### Setting an Audio Effect Mode for the Playback Instance
68
69Disable the system audio effect.
70
71  ```ts
72  import { BusinessError } from '@ohos.base';
73  audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_NONE, (err: BusinessError) => {
74    if (err) {
75      console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
76      return;
77    } else {
78      console.info('Callback invoked to indicate a successful audio effect mode setting.');
79    }
80  });
81  ```
82
83Enable the default system audio effect.
84
85  ```ts
86  import { BusinessError } from '@ohos.base';
87  audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => {
88    if (err) {
89      console.error(`Failed to set params, code is ${err.code}, message is ${err.message}`);
90      return;
91    } else {
92      console.info('Callback invoked to indicate a successful audio effect mode setting.');
93    }
94  });
95  ```
96
97## Obtaining the Global Audio Effect Mode
98
99You can obtain the global audio effect mode corresponding to a specific audio stream usage (specified by **StreamUsage**).
100For 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.
101
102### Creating an AudioStreamManager Instance
103
104Create an **AudioStreamManager** instance. Before using **AudioStreamManager** APIs, you must use **getStreamManager()** to create an **AudioStreamManager** instance.
105
106   ```ts
107   import audio from '@ohos.multimedia.audio';
108   let audioManager = audio.getAudioManager();
109   let audioStreamManager = audioManager.getStreamManager();
110   ```
111
112### Querying the Audio Effect Mode of the Corresponding Scenario
113
114  ```ts
115  import { BusinessError } from '@ohos.base';
116  audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MEDIA, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => {
117    if (err) {
118      console.error('Failed to get effect info array');
119      return;
120    } else {
121      console.info(`getAudioEffectInfoArray: ${audioEffectInfoArray}`);
122    }
123  });
124  ```
125