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; 23 private mAudioVolumeGroupManager: audio.AudioVolumeGroupManager; 24 private mAudioRoutingManager: audio.AudioRoutingManager; 25 26 async initManager(): Promise<void> { 27 this.mAudioManager = audio.getAudioManager(); 28 this.mAudioRoutingManager = this.mAudioManager.getRoutingManager(); 29 this.mAudioVolumeGroupManager = await this.mAudioManager.getVolumeManager().getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID); 30 } 31 32 async isMicrophoneMute(): Promise<boolean> { 33 let ret: boolean = false; 34 if (this.mAudioVolumeGroupManager) { 35 ret = await this.mAudioVolumeGroupManager.isMicrophoneMute(); 36 } 37 return ret; 38 } 39 40 //设置mic是否静音 41 async setMicrophoneMute(mute: boolean): Promise<void> { 42 Logger.info(`${TAG}: setMicrophoneMute ${mute} `); 43 try { 44 if (this.mAudioVolumeGroupManager) { 45 await this.mAudioVolumeGroupManager.setMicrophoneMute(mute); 46 Logger.info(`${TAG}: setMicrophoneMute sucess`); 47 } 48 } 49 catch (err) { 50 Logger.error(`${TAG}: setMicrophoneMute failed, code is ${err.code}, message is ${err.message}`); 51 } 52 } 53 /** 54 * 设置通话场景 55 AudioVolumeGroupManager.setAudioScene用于调整音频场景,属于系统接口。 56 目前版本(4.0.7.1之后)已实现自动切换AudioScene。 57 对于非系统应用,若使用AudioRenderer播放VOICE_CALL类型的音频, 58 则调用start函数时会自动切换至AUDIO_SCENE_VOICE_CHAT, 59 播放结束后自动切换回默认场景,无需调用setAudioScene。 60 */ 61 async setVoiceScene(calling: boolean): Promise<void> { 62 Logger.info(`${TAG}: setVoiceScene ${calling}`); 63 if (!this.mAudioManager) { 64 return; 65 } 66 try { 67 if (calling) { 68 await this.mAudioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_VOICE_CHAT); 69 Logger.info(`${TAG}: setVoiceScene AUDIO_SCENE_VOICE_CHAT sucess`); 70 } else { 71 await this.mAudioManager.setAudioScene(audio.AudioScene.AUDIO_SCENE_DEFAULT); 72 Logger.info(`${TAG}: setVoiceScene AUDIO_SCENE_DEFAULT sucess`); 73 } 74 } catch (err) { 75 Logger.error(`${TAG}: setVoiceScene failed, code is ${err.code}, message is ${err.message}`); 76 } 77 78 } 79 80 //设置是否打开扬声器 81 async setSpeaker(speaker: boolean): Promise<void> { 82 Logger.info(`${TAG}: setSpeaker ${speaker}`); 83 try { 84 if (!this.mAudioRoutingManager) { 85 return; 86 } 87 await this.mAudioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, speaker); 88 89 Logger.info(`${TAG}: setSpeaker sucess`); 90 } catch (err) { 91 Logger.error(`${TAG}: setSpeaker failed, code is ${err.code}, message is ${err.message}`); 92 } 93 } 94 95 //设置是否打开扬声器 96 async isSpeakerActive(): Promise<boolean> { 97 if (!this.mAudioRoutingManager) { 98 return false; 99 } 100 let isSpeakerActive:boolean = await this.mAudioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER); 101 Logger.info(`${TAG}: isSpeakerActive : ${isSpeakerActive}`); 102 return isSpeakerActive; 103 } 104} 105 106let mModel = new AudioManagerModel(); 107 108export default mModel as AudioManagerModel;