1e41f4b71Sopenharmony_ci# Audio Recording Stream Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciAn 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## Reading or Listening for Audio Stream State Changes in the Application
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciCreate 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:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Check the [state](../../reference/apis-audio-kit/js-apis-audio.md#attributes) of the AudioCapturer.
10e41f4b71Sopenharmony_ci    
11e41f4b71Sopenharmony_ci  ```ts
12e41f4b71Sopenharmony_ci  let audioCapturerState: audio.AudioState = audioCapturer.state;
13e41f4b71Sopenharmony_ci  console.info(`Current state is: ${audioCapturerState }`)
14e41f4b71Sopenharmony_ci  ```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci- Register **stateChange** to listen for state changes of the AudioCapturer.
17e41f4b71Sopenharmony_ci    
18e41f4b71Sopenharmony_ci  ```ts
19e41f4b71Sopenharmony_ci  audioCapturer.on('stateChange', (capturerState: audio.AudioState) => {
20e41f4b71Sopenharmony_ci    console.info(`State change to: ${capturerState}`)
21e41f4b71Sopenharmony_ci  });
22e41f4b71Sopenharmony_ci  ```
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciThe 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).
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci## Reading or Listening for Changes in All Audio Streams
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciIf 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.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci<!--Del-->
31e41f4b71Sopenharmony_ci> **NOTE**
32e41f4b71Sopenharmony_ci> 
33e41f4b71Sopenharmony_ci> The audio stream change information marked as the system API can be viewed only by system applications.
34e41f4b71Sopenharmony_ci<!--DelEnd-->
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciThe figure below shows the call relationship of audio stream management.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci![Call relationship of recording stream management](figures/invoking-relationship-recording-stream-mgmt.png)
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ciDuring 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.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciFor details about the APIs, see [AudioStreamManager](../../reference/apis-audio-kit/js-apis-audio.md#audiostreammanager9).
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci## How to Develop
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci1. Create an **AudioStreamManager** instance.
48e41f4b71Sopenharmony_ci   
49e41f4b71Sopenharmony_ci     Before using **AudioStreamManager** APIs, you must use **getStreamManager()** to create an **AudioStreamManager** instance.
50e41f4b71Sopenharmony_ci     
51e41f4b71Sopenharmony_ci   ```ts
52e41f4b71Sopenharmony_ci   import { audio } from '@kit.AudioKit';
53e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
54e41f4b71Sopenharmony_ci   
55e41f4b71Sopenharmony_ci   let audioManager = audio.getAudioManager();
56e41f4b71Sopenharmony_ci   let audioStreamManager = audioManager.getStreamManager();
57e41f4b71Sopenharmony_ci   ```
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci2. 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.
60e41f4b71Sopenharmony_ci     
61e41f4b71Sopenharmony_ci   ```ts
62e41f4b71Sopenharmony_ci   audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) =>  {
63e41f4b71Sopenharmony_ci     for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
64e41f4b71Sopenharmony_ci       console.info(`## CapChange on is called for element ${i} ##`);
65e41f4b71Sopenharmony_ci       console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
66e41f4b71Sopenharmony_ci       console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
67e41f4b71Sopenharmony_ci       console.info(`Flag  ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
68e41f4b71Sopenharmony_ci       let devDescriptor: audio.AudioDeviceDescriptors = AudioCapturerChangeInfoArray[i].deviceDescriptors;
69e41f4b71Sopenharmony_ci       for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
70e41f4b71Sopenharmony_ci         console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
71e41f4b71Sopenharmony_ci         console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
72e41f4b71Sopenharmony_ci         console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
73e41f4b71Sopenharmony_ci         console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
74e41f4b71Sopenharmony_ci         console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
75e41f4b71Sopenharmony_ci         console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
76e41f4b71Sopenharmony_ci         console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
77e41f4b71Sopenharmony_ci         console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
78e41f4b71Sopenharmony_ci       }
79e41f4b71Sopenharmony_ci     }
80e41f4b71Sopenharmony_ci   });
81e41f4b71Sopenharmony_ci   ```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci3. (Optional) Use **off('audioCapturerChange')** to cancel listening for audio recording stream changes.
84e41f4b71Sopenharmony_ci     
85e41f4b71Sopenharmony_ci   ```ts
86e41f4b71Sopenharmony_ci   audioStreamManager.off('audioCapturerChange');
87e41f4b71Sopenharmony_ci   console.info('CapturerChange Off is called');
88e41f4b71Sopenharmony_ci   ```
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci4. (Optional) Call **getCurrentAudioCapturerInfoArray()** to obtain information about the current audio recording stream.
91e41f4b71Sopenharmony_ci   
92e41f4b71Sopenharmony_ci     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.
93e41f4b71Sopenharmony_ci   > **NOTE**
94e41f4b71Sopenharmony_ci   > 
95e41f4b71Sopenharmony_ci   > 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.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci   ```ts
98e41f4b71Sopenharmony_ci   async function getCurrentAudioCapturerInfoArray(){
99e41f4b71Sopenharmony_ci     await audioStreamManager.getCurrentAudioCapturerInfoArray().then((AudioCapturerChangeInfoArray: audio.AudioCapturerChangeInfoArray) => {
100e41f4b71Sopenharmony_ci       console.info('getCurrentAudioCapturerInfoArray  Get Promise Called ');
101e41f4b71Sopenharmony_ci       if (AudioCapturerChangeInfoArray != null) {
102e41f4b71Sopenharmony_ci         for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
103e41f4b71Sopenharmony_ci           console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
104e41f4b71Sopenharmony_ci           console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
105e41f4b71Sopenharmony_ci           console.info(`Flag  ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
106e41f4b71Sopenharmony_ci           for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
107e41f4b71Sopenharmony_ci             console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
108e41f4b71Sopenharmony_ci             console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
109e41f4b71Sopenharmony_ci             console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
110e41f4b71Sopenharmony_ci             console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
111e41f4b71Sopenharmony_ci             console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
112e41f4b71Sopenharmony_ci             console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
113e41f4b71Sopenharmony_ci             console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
114e41f4b71Sopenharmony_ci             console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
115e41f4b71Sopenharmony_ci           }
116e41f4b71Sopenharmony_ci         }
117e41f4b71Sopenharmony_ci       }
118e41f4b71Sopenharmony_ci     }).catch((err: BusinessError) => {
119e41f4b71Sopenharmony_ci       console.error(`Invoke getCurrentAudioCapturerInfoArray failed, code is ${err.code}, message is ${err.message}`);
120e41f4b71Sopenharmony_ci     });
121e41f4b71Sopenharmony_ci   }
122e41f4b71Sopenharmony_ci   ```
123