W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
對(duì)于播放音頻類的應(yīng)用,開(kāi)發(fā)者需要關(guān)注該應(yīng)用的音頻流的狀態(tài)以做出相應(yīng)的操作,比如監(jiān)聽(tīng)到狀態(tài)為播放中/暫停時(shí),及時(shí)改變播放按鈕的UI顯示。
參考使用AudioRenderer開(kāi)發(fā)音頻播放功能或audio.createAudioRenderer,完成AudioRenderer的創(chuàng)建,然后可以通過(guò)以下兩種方式查看音頻流狀態(tài)的變化:
- let audioRendererState = audioRenderer.state;
- console.info(`Current state is: ${audioRendererState }`)
- audioRenderer.on('stateChange', (rendererState) => {
- console.info(`State change to: ${rendererState}`)
- });
獲取state后可對(duì)照AudioState來(lái)進(jìn)行相應(yī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。
在使用AudioStreamManager的API前,需要使用getStreamManager()創(chuàng)建一個(gè)AudioStreamManager實(shí)例。
- import audio from '@ohos.multimedia.audio';
- let audioManager = audio.getAudioManager();
- let audioStreamManager = audioManager.getStreamManager();
- audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => {
- for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) {
- let AudioRendererChangeInfo = AudioRendererChangeInfoArray[i];
- console.info(`## RendererChange on is called for ${i} ##`);
- console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`);
- console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`);
- console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`);
- console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`);
- for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) {
- console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`);
- console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`);
- console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`);
- console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`);
- console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`);
- console.info(`SampleRates: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`);
- console.info(`ChannelCount ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`);
- console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks}`);
- }
- }
- });
- audioStreamManager.off('audioRendererChange');
- console.info('RendererChange Off is called ');
對(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)屬性)將為空字符串。
- async function getCurrentAudioRendererInfoArray(){
- await audioStreamManager.getCurrentAudioRendererInfoArray().then( function (AudioRendererChangeInfoArray) {
- console.info(`getCurrentAudioRendererInfoArray Get Promise is called `);
- if (AudioRendererChangeInfoArray != null) {s
- for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) {
- let AudioRendererChangeInfo = AudioRendererChangeInfoArray[i];
- console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`);
- console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`);
- console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`);
- console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`);
- for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) {
- console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`);
- console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`);
- console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`);
- console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`);
- console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`);
- console.info(`SampleRates: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`);
- console.info(`ChannelCount ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`);
- console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks}`);
- }
- }
- }
- }).catch((err) => {
- console.error(`Invoke getCurrentAudioRendererInfoArray failed, code is ${err.code}, message is ${err.message}`);
- });
- }
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)系方式:
更多建議: