harmony 鸿蒙Audio Effect Management
Audio Effect Management
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.
Import the audio module.
import { audio } from '@kit.AudioKit';
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 '@kit.BasicServicesKit'; let audioStreamInfo: audio.AudioStreamInfo = { samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate. channels: audio.AudioChannel.CHANNEL_2, // Channel. sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format. encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format. }; let audioRendererInfo: audio.AudioRendererInfo = { usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario. rendererFlags: 0 // AudioRenderer flag. }; 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 '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
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 '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
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 '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
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, which is 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 '@kit.AudioKit';
let audioManager = audio.getAudioManager();
let audioStreamManager = audioManager.getStreamManager();
Querying the Audio Effect Mode of the Corresponding Scenario
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
audioStreamManager.getAudioEffectInfoArray(audio.StreamUsage.STREAM_USAGE_MUSIC, async (err: BusinessError, audioEffectInfoArray: audio.AudioEffectInfoArray) => {
if (err) {
console.error('Failed to get effect info array');
return;
} else {
console.info(`getAudioEffectInfoArray: ${audioEffectInfoArray}`);
}
});
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Developing Audio Call
harmony 鸿蒙Global Audio Input Device Management
harmony 鸿蒙Introduction to Audio Kit
harmony 鸿蒙Audio Latency Management
harmony 鸿蒙Responding to Audio Output Device Changes
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦