1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import Logger from '../utils/Logger'; 16 17import audio from '@ohos.multimedia.audio'; 18 19const TAG = 'AudioManagerModel'; 20 21class AudioManagerModel { 22 private mAudioManager: audio.AudioManager | null = null; 23 private mAudioVolumeGroupManager: audio.AudioVolumeGroupManager | null = null; 24 private mAudioRoutingManager: audio.AudioRoutingManager | null = null; 25 26 async initManager(): Promise<void> { 27 this.mAudioManager = audio.getAudioManager(); 28 this.mAudioRoutingManager = this.mAudioManager.getRoutingManager(); 29 this.mAudioVolumeGroupManager = await this.mAudioManager.getVolumeManager() 30 .getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID); 31 } 32 33 async isMicrophoneMute(): Promise<boolean> { 34 let ret: boolean = false; 35 if (this.mAudioVolumeGroupManager) { 36 ret = await this.mAudioVolumeGroupManager.isMicrophoneMute(); 37 } 38 return ret; 39 } 40 41 //设置mic是否静音 42 async setMicrophoneMute(mute: boolean): Promise<void> { 43 Logger.info(TAG,` setMicrophoneMute ${mute} `); 44 try { 45 if (this.mAudioVolumeGroupManager) { 46 await this.mAudioVolumeGroupManager.setMicrophoneMute(mute); 47 Logger.info(TAG,` setMicrophoneMute sucess`); 48 } 49 } 50 catch (err) { 51 Logger.error(TAG,`setMicrophoneMute failed, code is ${err.code}, message is ${err.message}`); 52 } 53 } 54 55 /** 56 * 设置通话场景 57 AudioVolumeGroupManager.setAudioScene用于调整音频场景,属于系统接口。 58 目前版本(4.0.7.1之后)已实现自动切换AudioScene。 59 对于非系统应用,若使用AudioRenderer播放VOICE_CALL类型的音频, 60 则调用start函数时会自动切换至AUDIO_SCENE_VOICE_CHAT, 61 播放结束后自动切换回默认场景,无需调用setAudioScene。 62 */ 63 async setVoiceScene(calling: boolean): Promise<void> { 64 Logger.info(TAG,`setVoiceScene ${calling}`); 65 if (!this.mAudioManager) { 66 return; 67 } 68 try { 69 if (calling) { 70 await this.mAudioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_VOICE_CHAT); 71 Logger.info(TAG,`setVoiceScene AUDIO_SCENE_VOICE_CHAT sucess`); 72 } else { 73 await this.mAudioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_DEFAULT); 74 Logger.info(TAG,`setVoiceScene AUDIO_SCENE_DEFAULT sucess`); 75 } 76 } catch (err) { 77 Logger.error(TAG,`setVoiceScene failed, code is ${err.code}, message is ${err.message}`); 78 } 79 80 } 81 82 //设置是否打开扬声器 83 async setSpeaker(speaker: boolean): Promise<void> { 84 Logger.info(TAG,`setSpeaker ${speaker}`); 85 try { 86 if (!this.mAudioRoutingManager) { 87 return; 88 } 89 await this.mAudioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, speaker); 90 91 Logger.info(TAG,` setSpeaker sucess`); 92 } catch (err) { 93 Logger.error(TAG,`setSpeaker failed, code is ${err.code}, message is ${err.message}`); 94 } 95 } 96 97 //设置是否打开扬声器 98 async isSpeakerActive(): Promise<boolean> { 99 if (!this.mAudioRoutingManager) { 100 return false; 101 } 102 let isSpeakerActive: boolean = await this.mAudioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER); 103 Logger.info(TAG,` isSpeakerActive : ${isSpeakerActive}`); 104 return isSpeakerActive; 105 } 106} 107 108let mModel = new AudioManagerModel(); 109 110export default mModel as AudioManagerModel;