W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
對于錄制音頻類的應(yīng)用,開發(fā)者需要關(guān)注該應(yīng)用的音頻流的狀態(tài)以做出相應(yīng)的操作,比如監(jiān)聽到狀態(tài)為結(jié)束時,及時提示用戶錄制已結(jié)束。
參考使用AudioCapturer開發(fā)音頻錄制功能或audio.createAudioCapturer,完成AudioCapturer的創(chuàng)建,然后可以通過以下兩種方式查看音頻流狀態(tài)的變化:
- let audioCapturerState = audioCapturer.state;
- console.info(`Current state is: ${audioCapturerState }`)
- audioCapturer.on('stateChange', (capturerState) => {
- console.info(`State change to: ${capturerState}`)
- });
獲取state后可對照AudioState來進(jìn)行相應(yīng)的操作,比如顯示錄制結(jié)束的提示等。
如果部分應(yīng)用需要查詢獲取所有音頻流的變化信息,可以通過AudioStreamManager讀取或監(jiān)聽所有音頻流的變化。
如下為音頻流管理調(diào)用關(guān)系圖:
在進(jìn)行應(yīng)用開發(fā)的過程中,開發(fā)者需要使用getStreamManager()創(chuàng)建一個AudioStreamManager實(shí)例,進(jìn)而通過該實(shí)例管理音頻流。開發(fā)者可通過調(diào)用on('audioCapturerChange')監(jiān)聽音頻流的變化,在音頻流狀態(tài)變化、設(shè)備變化時獲得通知,同時可通過off('audioCapturerChange')取消相關(guān)事件的監(jiān)聽。另外,開發(fā)者可以通過主動調(diào)用getCurrentAudioCapturerInfoArray()查詢錄制流的唯一ID、錄制流客戶端的UID、以及流狀態(tài)等信息。
詳細(xì)API含義可參考音頻管理API文檔AudioStreamManager。
- import audio from '@ohos.multimedia.audio';
- let audioManager = audio.getAudioManager();
- let audioStreamManager = audioManager.getStreamManager();
- audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) => {
- for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
- console.info(`## CapChange on is called for element ${i} ##`);
- console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
- console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
- console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
- let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors;
- for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
- console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
- console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
- console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
- console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
- console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
- console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
- console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
- console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
- }
- }
- });
- audioStreamManager.off('audioCapturerChange');
- console.info('CapturerChange Off is called');
對所有音頻流狀態(tài)進(jìn)行監(jiān)聽的應(yīng)用需要申請權(quán)限ohos.permission.USE_BLUETOOTH,否則無法獲得實(shí)際的設(shè)備名稱和設(shè)備地址信息,查詢到的設(shè)備名稱和設(shè)備地址(藍(lán)牙設(shè)備的相關(guān)屬性)將為空字符串。
- async function getCurrentAudioCapturerInfoArray(){
- await audioStreamManager.getCurrentAudioCapturerInfoArray().then( function (AudioCapturerChangeInfoArray) {
- console.info('getCurrentAudioCapturerInfoArray **** Get Promise Called ****');
- if (AudioCapturerChangeInfoArray != null) {
- for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
- console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
- console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
- console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
- for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
- console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
- console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
- console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
- console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
- console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
- console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
- console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
- console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
- }
- }
- }
- }).catch((err) => {
- console.error(`Invoke getCurrentAudioCapturerInfoArray failed, code is ${err.code}, message is ${err.message}`);
- });
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: