W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
因為在錄制過程中需要使用麥克風錄制相關音頻數(shù)據(jù),所以建議開發(fā)者在調(diào)用錄制接口前查詢麥克風狀態(tài),并在錄制過程中監(jiān)聽麥克風的狀態(tài)變化,避免影響錄制效果。
在音頻錄制過程中,用戶可以將麥克風靜音,此時錄音過程正常進行,錄制生成的數(shù)據(jù)文件的大小隨錄制時長遞增,但寫入文件的數(shù)據(jù)均為0,即無聲數(shù)據(jù)(空白數(shù)據(jù))。
在AudioVolumeGroupManager中提供了管理麥克風狀態(tài)的方法,接口的詳細說明請參考API文檔。
- import audio from '@ohos.multimedia.audio';
- let audioVolumeGroupManager;
- async function loadVolumeGroupManager() { //創(chuàng)建audioVolumeGroupManager對象
- const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
- audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
- console.info('audioVolumeGroupManager create success.');
- }
目前此訂閱接口在單進程多AudioManager實例的使用場景下,僅最后一個實例的訂閱生效,其他實例的訂閱會被覆蓋(即使最后一個實例沒有進行訂閱),因此推薦使用單一AudioManager實例進行開發(fā)。
- async function on() { //監(jiān)聽麥克風狀態(tài)變化
- audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
- console.info(`Current microphone status is: ${micStateChange.mute} `);
- });
- }
- async function isMicrophoneMute() { //查詢麥克風是否靜音
- await audioVolumeGroupManager.isMicrophoneMute().then((value) => {
- console.info(`isMicrophoneMute is: ${value}.`);
- });
- }
- async function setMicrophoneMuteTrue() { //設置麥克風靜音,入?yún)閠rue
- await audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
- console.info('setMicrophoneMute to mute.');
- });
- }
- async function setMicrophoneMuteFalse() { //取消麥克風靜音,入?yún)閒alse
- await audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
- console.info('setMicrophoneMute to not mute.');
- });
- }
參考以下示例,完成從設置麥克風靜音到取消麥克風靜音的過程。
- import audio from '@ohos.multimedia.audio';
- @Entry
- @Component
- struct AudioVolumeGroup {
- private audioVolumeGroupManager: audio.AudioVolumeGroupManager;
- async loadVolumeGroupManager() {
- const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
- this.audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
- console.info('audioVolumeGroupManager------create-------success.');
- }
- async on() { //監(jiān)聽麥克風狀態(tài)變化
- await this.loadVolumeGroupManager();
- this.audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
- console.info(`Current microphone status is: ${micStateChange.mute} `);
- });
- }
- async isMicrophoneMute() { //查詢麥克風是否靜音
- await this.audioVolumeGroupManager.isMicrophoneMute().then((value) => {
- console.info(`isMicrophoneMute is: ${value}.`);
- });
- }
- async setMicrophoneMuteTrue() { //設置麥克風靜音
- await this.loadVolumeGroupManager();
- await this.audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
- console.info('setMicrophoneMute to mute.');
- });
- }
- async setMicrophoneMuteFalse() { //取消麥克風靜音
- await this.loadVolumeGroupManager();
- await this.audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
- console.info('setMicrophoneMute to not mute.');
- });
- }
- async test(){
- await this.on();
- await this.isMicrophoneMute();
- await this.setMicrophoneMuteTrue();
- await this.isMicrophoneMute();
- await this.setMicrophoneMuteFalse();
- await this.isMicrophoneMute();
- await this.setMicrophoneMuteTrue();
- await this.isMicrophoneMute();
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: