音頻播放流管理

2024-02-16 13:54 更新

對(duì)于播放音頻類的應(yīng)用,開(kāi)發(fā)者需要關(guān)注該應(yīng)用的音頻流的狀態(tài)以做出相應(yīng)的操作,比如監(jiān)聽(tīng)到狀態(tài)為播放中/暫停時(shí),及時(shí)改變播放按鈕的UI顯示。

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

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

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

獲取state后可對(duì)照AudioState來(lái)進(jìn)行相應(yīng)的操作,比如更改暫停播放按鈕的顯示等。

讀取或監(jiān)聽(tīng)所有音頻流的變化

如果部分應(yīng)用需要查詢獲取所有音頻流的變化信息,可以通過(guò)AudioStreamManager讀取或監(jiān)聽(tīng)所有音頻流的變化。

如下為音頻流管理調(diào)用關(guān)系圖:

在進(jìn)行應(yīng)用開(kāi)發(fā)的過(guò)程中,開(kāi)發(fā)者需要使用getStreamManager()創(chuàng)建一個(gè)AudioStreamManager實(shí)例,進(jìn)而通過(guò)該實(shí)例管理音頻流。開(kāi)發(fā)者可通過(guò)調(diào)用on('audioRendererChange')監(jiān)聽(tīng)音頻流的變化,在音頻流狀態(tài)變化、設(shè)備變化時(shí)獲得通知。同時(shí)可通過(guò)off('audioRendererChange')取消相關(guān)事件的監(jiān)聽(tīng)。另外,開(kāi)發(fā)者可以主動(dòng)調(diào)用getCurrentAudioRendererInfoArray()來(lái)查詢播放流的唯一ID、播放流客戶端的UID、音頻流狀態(tài)等信息。

詳細(xì)API含義可參考音頻管理API文檔AudioStreamManager。

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

  1. 創(chuàng)建AudioStreamManager實(shí)例。

    在使用AudioStreamManager的API前,需要使用getStreamManager()創(chuàng)建一個(gè)AudioStreamManager實(shí)例。

    1. import audio from '@ohos.multimedia.audio';
    2. let audioManager = audio.getAudioManager();
    3. let audioStreamManager = audioManager.getStreamManager();
  2. 使用on('audioRendererChange')監(jiān)聽(tīng)音頻播放流的變化。 如果音頻流監(jiān)聽(tīng)?wèi)?yīng)用需要在音頻播放流狀態(tài)變化、設(shè)備變化時(shí)獲取通知,可以訂閱該事件。
    1. audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => {
    2. for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) {
    3. let AudioRendererChangeInfo = AudioRendererChangeInfoArray[i];
    4. console.info(`## RendererChange on is called for ${i} ##`);
    5. console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`);
    6. console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`);
    7. console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`);
    8. console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`);
    9. for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) {
    10. console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`);
    11. console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`);
    12. console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`);
    13. console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`);
    14. console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`);
    15. console.info(`SampleRates: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`);
    16. console.info(`ChannelCount ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`);
    17. console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks}`);
    18. }
    19. }
    20. });
  3. (可選)使用off('audioRendererChange')取消監(jiān)聽(tīng)音頻播放流變化。
    1. audioStreamManager.off('audioRendererChange');
    2. console.info('RendererChange Off is called ');
  4. (可選)使用getCurrentAudioRendererInfoArray()獲取所有音頻播放流的信息。
    該接口可獲取音頻播放流唯一ID,音頻播放客戶端的UID,音頻狀態(tài)以及音頻播放器的其他信息。
    說(shuō)明

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

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)