harmony 鸿蒙Microphone Management
Microphone Management
The microphone is used to record audio data. To deliver an optimal recording effect, you are advised to query the microphone state before starting recording and listen for state changes during recording.
If the user mutes the microphone during audio recording, the recording process is normal, the size of the recorded file increases with the recording duration, but the data volume written into the file is 0.
How to Develop
The AudioVolumeGroupManager class provides APIs for managing the microphone state. For details, see API Reference.
- Create an audioVolumeGroupManager object.
import { audio } from '@kit.AudioKit';
let audioVolumeGroupManager: audio.AudioVolumeGroupManager;
// Create an audioVolumeGroupManager object.
async function loadVolumeGroupManager() {
const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
console.info('audioVolumeGroupManager create success.');
}
- (Optional; for system applications only) Call on(‘micStateChange’) to listen for microphone state changes. When the microphone state changes, the application will be notified of the change.
Currently, when multiple AudioManager instances are used in a single process, only the subscription of the last instance takes effect, and the subscription of other instances is overwritten (even if the last instance does not initiate a subscription). Therefore, you are advised to use a single AudioManager instance.
// Listen for microphone state changes.
async function on() {
audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => {
console.info(`Current microphone status is: ${micStateChange.mute} `);
});
}
- Call isMicrophoneMute to check whether the microphone is muted. If the return value is true, the microphone is muted; otherwise, the microphone is not muted.
// Check whether the microphone is muted.
async function isMicrophoneMute() {
await audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => {
console.info(`isMicrophoneMute is: ${value}.`);
});
}
- (Optional; for system applications only) Call setMicrophoneMute to mute or unmute the microphone. To mute the microphone, pass in true. To unmute the microphone, pass in false.
// Mute the microphone, with true passed.
async function setMicrophoneMuteTrue() {
await audioVolumeGroupManager.setMicMute(true).then(() => {
console.info('setMicrophoneMute to mute.');
});
}
// Unmute the microphone, with false passed.
async function setMicrophoneMuteFalse() {
await audioVolumeGroupManager.setMicMute(false).then(() => {
console.info('setMicrophoneMute to not mute.');
});
}
Sample Code
Refer to the sample code below to complete the process of muting and unmuting the microphone.
import { audio } from '@kit.AudioKit';
let audioVolumeGroupManager: audio.AudioVolumeGroupManager;
async function loadVolumeGroupManager() {
const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
console.info('audioVolumeGroupManager------create-------success.');
}
// Listen for microphone state changes.
async function on() {
await loadVolumeGroupManager();
audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
console.info(`Current microphone status is: ${micStateChange.mute} `);
});
}
// Check whether the microphone is muted.
async function isMicrophoneMute() {
await audioVolumeGroupManager.isMicrophoneMute().then((value) => {
console.info(`isMicrophoneMute is: ${value}.`);
});
}
// Mute the microphone.
async function setMicrophoneMuteTrue() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.setMicMute(true).then(() => {
console.info('setMicrophoneMute to mute.');
});
}
// Unmute the microphone.
async function setMicrophoneMuteFalse() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.setMicMute(false).then(() => {
console.info('setMicrophoneMute to not mute.');
});
}
async function test(){
await on();
await isMicrophoneMute();
await setMicrophoneMuteTrue();
await isMicrophoneMute();
await setMicrophoneMuteFalse();
await isMicrophoneMute();
await setMicrophoneMuteTrue();
await isMicrophoneMute();
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Developing Audio Call
harmony 鸿蒙Audio Effect Management
harmony 鸿蒙Global Audio Input Device Management
harmony 鸿蒙Introduction to Audio Kit
harmony 鸿蒙Audio Latency Management
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦