1# Audio Recording Stream Management 2 3An audio recording application must notice audio stream state changes and perform corresponding operations. For example, when detecting that the user stops recording, the application must notify the user that the recording finishes. 4 5## Reading or Listening for Audio Stream State Changes in the Application 6 7Create an AudioCapturer by referring to [Using AudioCapturer for Audio Recording](using-audiocapturer-for-recording.md) or [audio.createAudioCapturer](../../reference/apis-audio-kit/js-apis-audio.md#audiocreateaudiocapturer8). Then obtain the audio stream state changes in either of the following ways: 8 9- Check the [state](../../reference/apis-audio-kit/js-apis-audio.md#attributes) of the AudioCapturer. 10 11 ```ts 12 let audioCapturerState: audio.AudioState = audioCapturer.state; 13 console.info(`Current state is: ${audioCapturerState }`) 14 ``` 15 16- Register **stateChange** to listen for state changes of the AudioCapturer. 17 18 ```ts 19 audioCapturer.on('stateChange', (capturerState: audio.AudioState) => { 20 console.info(`State change to: ${capturerState}`) 21 }); 22 ``` 23 24The application then performs an operation, for example, displays a message indicating the end of the recording, by comparing the obtained state with [AudioState](../../reference/apis-audio-kit/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<!--Del--> 31> **NOTE** 32> 33> The audio stream change information marked as the system API can be viewed only by system applications. 34<!--DelEnd--> 35 36The figure below shows the call relationship of audio stream management. 37 38 39 40During application development, first use **getStreamManager()** to create an **AudioStreamManager** instance. Then call **on('audioCapturerChange')** 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('audioCapturerChange')**. You can call **getCurrentAudioCapturerInfoArray()** to obtain information such as the unique ID of the recording stream, UID of the recording stream client, and stream status. 41 42For details about the APIs, see [AudioStreamManager](../../reference/apis-audio-kit/js-apis-audio.md#audiostreammanager9). 43 44 45## How to Develop 46 471. Create an **AudioStreamManager** instance. 48 49 Before using **AudioStreamManager** APIs, you must use **getStreamManager()** to create an **AudioStreamManager** instance. 50 51 ```ts 52 import { audio } from '@kit.AudioKit'; 53 import { BusinessError } from '@kit.BasicServicesKit'; 54 55 let audioManager = audio.getAudioManager(); 56 let audioStreamManager = audioManager.getStreamManager(); 57 ``` 58 592. Use **on('audioCapturerChange')** to listen for audio recording stream changes. If the application needs to receive a notification when the audio recording stream state or device changes, it can subscribe to this event. 60 61 ```ts 62 audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 63 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 64 console.info(`## CapChange on is called for element ${i} ##`); 65 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 66 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 67 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 68 let devDescriptor: audio.AudioDeviceDescriptors = AudioCapturerChangeInfoArray[i].deviceDescriptors; 69 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 70 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 71 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 72 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 73 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 74 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 75 console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 76 console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 77 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`); 78 } 79 } 80 }); 81 ``` 82 833. (Optional) Use **off('audioCapturerChange')** to cancel listening for audio recording stream changes. 84 85 ```ts 86 audioStreamManager.off('audioCapturerChange'); 87 console.info('CapturerChange Off is called'); 88 ``` 89 904. (Optional) Call **getCurrentAudioCapturerInfoArray()** to obtain information about the current audio recording stream. 91 92 This API can be used to obtain the unique ID of the audio recording stream, UID of the audio recording client, audio status, and other information about the AudioCapturer. 93 > **NOTE** 94 > 95 > Before listening for state changes of all audio streams, the application must [declare the ohos.permission.USE_BLUETOOTH permission](../../security/AccessToken/declare-permissions.md), for the device name and device address (Bluetooth related attributes) to be displayed correctly. 96 97 ```ts 98 async function getCurrentAudioCapturerInfoArray(){ 99 await audioStreamManager.getCurrentAudioCapturerInfoArray().then((AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => { 100 console.info('getCurrentAudioCapturerInfoArray Get Promise Called '); 101 if (AudioCapturerChangeInfoArray != null) { 102 for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) { 103 console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`); 104 console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`); 105 console.info(`Flag ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`); 106 for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) { 107 console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`); 108 console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`); 109 console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`); 110 console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`); 111 console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`); 112 console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`); 113 console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`); 114 console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`); 115 } 116 } 117 } 118 }).catch((err: BusinessError) => { 119 console.error(`Invoke getCurrentAudioCapturerInfoArray failed, code is ${err.code}, message is ${err.message}`); 120 }); 121 } 122 ``` 123