音頻錄制流管理

2024-02-16 13:56 更新

對于錄制音頻類的應(yīng)用,開發(fā)者需要關(guān)注該應(yīng)用的音頻流的狀態(tài)以做出相應(yīng)的操作,比如監(jiān)聽到狀態(tài)為結(jié)束時,及時提示用戶錄制已結(jié)束。

讀取或監(jiān)聽?wèi)?yīng)用內(nèi)音頻流狀態(tài)變化

參考使用AudioCapturer開發(fā)音頻錄制功能audio.createAudioCapturer,完成AudioCapturer的創(chuàng)建,然后可以通過以下兩種方式查看音頻流狀態(tài)的變化:

  • 方法1:直接查看AudioCapturer的state
    1. let audioCapturerState = audioCapturer.state;
    2. console.info(`Current state is: ${audioCapturerState }`)
  • 方法2:注冊stateChange監(jiān)聽AudioCapturer的狀態(tài)變化:
    1. audioCapturer.on('stateChange', (capturerState) => {
    2. console.info(`State change to: ${capturerState}`)
    3. });

獲取state后可對照AudioState來進(jìn)行相應(yīng)的操作,比如顯示錄制結(jié)束的提示等。

讀取或監(jiān)聽所有錄制流的變化

如果部分應(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。

開發(fā)步驟及注意事項(xiàng)

  1. 創(chuàng)建AudioStreamManager實(shí)例。
    在使用AudioStreamManager的API前,需要使用getStreamManager()創(chuàng)建一個AudioStreamManager實(shí)例。
    1. import audio from '@ohos.multimedia.audio';
    2. let audioManager = audio.getAudioManager();
    3. let audioStreamManager = audioManager.getStreamManager();
  2. 使用on('audioCapturerChange')監(jiān)聽音頻錄制流更改事件。 如果音頻流監(jiān)聽?wèi)?yīng)用需要在音頻錄制流狀態(tài)變化、設(shè)備變化時獲取通知,可以訂閱該事件。
    1. audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) => {
    2. for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
    3. console.info(`## CapChange on is called for element ${i} ##`);
    4. console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
    5. console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
    6. console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
    7. let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors;
    8. for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
    9. console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
    10. console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
    11. console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
    12. console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
    13. console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
    14. console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
    15. console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
    16. console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
    17. }
    18. }
    19. });
  3. (可選)使用off('audioCapturerChange')取消監(jiān)聽音頻錄制流變化。
    1. audioStreamManager.off('audioCapturerChange');
    2. console.info('CapturerChange Off is called');
  4. (可選)使用getCurrentAudioCapturerInfoArray()獲取當(dāng)前音頻錄制流的信息。
    該接口可獲取音頻錄制流唯一ID,音頻錄制客戶端的UID,音頻狀態(tài)以及音頻捕獲器的其他信息。
    說明

    對所有音頻流狀態(tài)進(jìn)行監(jiān)聽的應(yīng)用需要申請權(quán)限ohos.permission.USE_BLUETOOTH,否則無法獲得實(shí)際的設(shè)備名稱和設(shè)備地址信息,查詢到的設(shè)備名稱和設(shè)備地址(藍(lán)牙設(shè)備的相關(guān)屬性)將為空字符串。

    1. async function getCurrentAudioCapturerInfoArray(){
    2. await audioStreamManager.getCurrentAudioCapturerInfoArray().then( function (AudioCapturerChangeInfoArray) {
    3. console.info('getCurrentAudioCapturerInfoArray **** Get Promise Called ****');
    4. if (AudioCapturerChangeInfoArray != null) {
    5. for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
    6. console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
    7. console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
    8. console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
    9. for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
    10. console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
    11. console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
    12. console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
    13. console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
    14. console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
    15. console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
    16. console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
    17. console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
    18. }
    19. }
    20. }
    21. }).catch((err) => {
    22. console.error(`Invoke getCurrentAudioCapturerInfoArray failed, code is ${err.code}, message is ${err.message}`);
    23. });
    24. }
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號