1# Audio Playback Stream Management 2 3An audio playback application must notice audio stream state changes and perform corresponding operations. For example, when detecting that an audio stream is being played or paused, the application must change the UI display of the **Play** button. 4 5## Reading or Listening for Audio Stream State Changes in the Application 6 7Create an AudioRenderer by referring to [Using AudioRenderer for Audio Playback](using-audiorenderer-for-playback.md) or [audio.createAudioRenderer](../reference/apis/js-apis-audio.md#audiocreateaudiorenderer8). Then obtain the audio stream state changes in either of the following ways: 8 9- Check the [state](../reference/apis/js-apis-audio.md#attributes) of the AudioRenderer. 10 11 ```ts 12 let audioRendererState = audioRenderer.state; 13 console.info(`Current state is: ${audioRendererState }`) 14 ``` 15 16- Register **stateChange** to listen for state changes of the AudioRenderer. 17 18 ```ts 19 audioRenderer.on('stateChange', (rendererState) => { 20 console.info(`State change to: ${rendererState}`) 21 }); 22 ``` 23 24The application then performs an operation, for example, changing the display of the **Play** button, by comparing the obtained state with [AudioState](../reference/apis/js-apis-audio.md#audiostate8). 25 26## Reading or Listening for Changes in All Audio Streams 27 28If an application needs to obtain the change information about all audio streams, it can use **AudioStreamManager** to read or listen for the changes of all audio streams. 29 30> **NOTE** 31> 32> The audio stream change information marked as the system API can be viewed only by system applications. 33 34The figure below shows the call relationship of audio stream management. 35 36![Call relationship of audio stream management](figures/audio-stream-mgmt-invoking-relationship.png) 37 38During application development, first use **getStreamManager()** to create an **AudioStreamManager** instance. Then call **on('audioRendererChange')** to listen for audio stream changes and obtain a notification when the audio stream state or device changes. To cancel the listening for these changes, call **off('audioRendererChange')**. You can also call **getCurrentAudioRendererInfoArray()** to obtain information such as the unique ID of the playback stream, UID of the playback stream client, and stream status. 39 40For details about the APIs, see [AudioStreamManager](../reference/apis/js-apis-audio.md#audiostreammanager9). 41 42## How to Develop 43 441. Create an **AudioStreamManager** instance. 45 46 Before using **AudioStreamManager** APIs, you must use **getStreamManager()** to create an **AudioStreamManager** instance. 47 48 ```ts 49 import audio from '@ohos.multimedia.audio'; 50 let audioManager = audio.getAudioManager(); 51 let audioStreamManager = audioManager.getStreamManager(); 52 ``` 53 542. Use **on('audioRendererChange')** to listen for audio playback stream changes. If the application needs to receive a notification when the audio playback stream state or device changes, it can subscribe to this event. 55 56 ```ts 57 audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray) => { 58 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 59 let AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 60 console.info(`## RendererChange on is called for ${i} ##`); 61 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 62 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 63 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 64 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 65 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 66 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 67 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 68 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 69 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 70 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 71 console.info(`SampleRates: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 72 console.info(`ChannelCount ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 73 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks}`); 74 } 75 } 76 }); 77 ``` 78 793. (Optional) Use **off('audioRendererChange')** to cancel listening for audio playback stream changes. 80 81 ```ts 82 audioStreamManager.off('audioRendererChange'); 83 console.info('RendererChange Off is called '); 84 ``` 85 864. (Optional) Call **getCurrentAudioRendererInfoArray()** to obtain the information about all audio playback streams. 87 88 This API can be used to obtain the unique ID of the audio playback stream, UID of the audio playback client, audio status, and other information about the audio player. 89 > **NOTE** 90 > 91 > Before listening for state changes of all audio streams, the application must request the **ohos.permission.USE_BLUETOOTH** [permission](../security/accesstoken-guidelines.md), for the device name and device address (Bluetooth related attributes) to be displayed correctly. 92 93 ```ts 94 async function getCurrentAudioRendererInfoArray(){ 95 await audioStreamManager.getCurrentAudioRendererInfoArray().then( function (AudioRendererChangeInfoArray) { 96 console.info(`getCurrentAudioRendererInfoArray Get Promise is called `); 97 if (AudioRendererChangeInfoArray != null) { 98 for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) { 99 let AudioRendererChangeInfo = AudioRendererChangeInfoArray[i]; 100 console.info(`StreamId for ${i} is: ${AudioRendererChangeInfo.streamId}`); 101 console.info(`Content ${i} is: ${AudioRendererChangeInfo.rendererInfo.content}`); 102 console.info(`Stream ${i} is: ${AudioRendererChangeInfo.rendererInfo.usage}`); 103 console.info(`Flag ${i} is: ${AudioRendererChangeInfo.rendererInfo.rendererFlags}`); 104 for (let j = 0;j < AudioRendererChangeInfo.deviceDescriptors.length; j++) { 105 console.info(`Id: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].id}`); 106 console.info(`Type: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceType}`); 107 console.info(`Role: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].deviceRole}`); 108 console.info(`Name: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].name}`); 109 console.info(`Address: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].address}`); 110 console.info(`SampleRates: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].sampleRates[0]}`); 111 console.info(`ChannelCount ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelCounts[0]}`); 112 console.info(`ChannelMask: ${i} : ${AudioRendererChangeInfo.deviceDescriptors[j].channelMasks}`); 113 } 114 } 115 } 116 }).catch((err) => { 117 console.error(`Invoke getCurrentAudioRendererInfoArray failed, code is ${err.code}, message is ${err.message}`); 118 }); 119 } 120 ``` 121