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