管理麦克风

2024-02-16 13:56 更新

因为在录制过程中需要使用麦克风录制相关音频数据,所以建议开发者在调用录制接口前查询麦克风状态,并在录制过程中监听麦克风的状态变化,避免影响录制效果。

在音频录制过程中,用户可以将麦克风静音,此时录音过程正常进行,录制生成的数据文件的大小随录制时长递增,但写入文件的数据均为0,即无声数据(空白数据)。

开发步骤及注意事项

在AudioVolumeGroupManager中提供了管理麦克风状态的方法,接口的详细说明请参考API文档

  1. 创建audioVolumeGroupManager对象。
    1. import audio from '@ohos.multimedia.audio';
    2. let audioVolumeGroupManager;
    3. async function loadVolumeGroupManager() { //创建audioVolumeGroupManager对象
    4. const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
    5. audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
    6. console.info('audioVolumeGroupManager create success.');
    7. }
  2. 调用on('micStateChange')监听麦克风状态变化,当麦克风静音状态发生变化时将通知应用。

    目前此订阅接口在单进程多AudioManager实例的使用场景下,仅最后一个实例的订阅生效,其他实例的订阅会被覆盖(即使最后一个实例没有进行订阅),因此推荐使用单一AudioManager实例进行开发。

    1. async function on() { //监听麦克风状态变化
    2. audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
    3. console.info(`Current microphone status is: ${micStateChange.mute} `);
    4. });
    5. }
  3. 调用isMicrophoneMute查询麦克风当前静音状态,返回true为静音,false为非静音。
    1. async function isMicrophoneMute() { //查询麦克风是否静音
    2. await audioVolumeGroupManager.isMicrophoneMute().then((value) => {
    3. console.info(`isMicrophoneMute is: ${value}.`);
    4. });
    5. }
  4. 根据查询结果的实际情况,调用setMicrophoneMute设置麦克风静音状态,入参输入true为静音,false为非静音。
    1. async function setMicrophoneMuteTrue() { //设置麦克风静音,入参为true
    2. await audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
    3. console.info('setMicrophoneMute to mute.');
    4. });
    5. }
    6. async function setMicrophoneMuteFalse() { //取消麦克风静音,入参为false
    7. await audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
    8. console.info('setMicrophoneMute to not mute.');
    9. });
    10. }

完整示例

参考以下示例,完成从设置麦克风静音到取消麦克风静音的过程。

  1. import audio from '@ohos.multimedia.audio';
  2. @Entry
  3. @Component
  4. struct AudioVolumeGroup {
  5. private audioVolumeGroupManager: audio.AudioVolumeGroupManager;
  6. async loadVolumeGroupManager() {
  7. const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
  8. this.audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
  9. console.info('audioVolumeGroupManager------create-------success.');
  10. }
  11. async on() { //监听麦克风状态变化
  12. await this.loadVolumeGroupManager();
  13. this.audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
  14. console.info(`Current microphone status is: ${micStateChange.mute} `);
  15. });
  16. }
  17. async isMicrophoneMute() { //查询麦克风是否静音
  18. await this.audioVolumeGroupManager.isMicrophoneMute().then((value) => {
  19. console.info(`isMicrophoneMute is: ${value}.`);
  20. });
  21. }
  22. async setMicrophoneMuteTrue() { //设置麦克风静音
  23. await this.loadVolumeGroupManager();
  24. await this.audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
  25. console.info('setMicrophoneMute to mute.');
  26. });
  27. }
  28. async setMicrophoneMuteFalse() { //取消麦克风静音
  29. await this.loadVolumeGroupManager();
  30. await this.audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
  31. console.info('setMicrophoneMute to not mute.');
  32. });
  33. }
  34. async test(){
  35. await this.on();
  36. await this.isMicrophoneMute();
  37. await this.setMicrophoneMuteTrue();
  38. await this.isMicrophoneMute();
  39. await this.setMicrophoneMuteFalse();
  40. await this.isMicrophoneMute();
  41. await this.setMicrophoneMuteTrue();
  42. await this.isMicrophoneMute();
  43. }
  44. }
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号