• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 音频录制流管理
2
3对于录制音频类的应用,开发者需要关注该应用的音频流的状态以做出相应的操作,比如监听到状态为结束时,及时提示用户录制已结束。
4
5## 读取或监听应用内音频流状态变化
6
7参考[使用AudioCapturer开发音频录制功能](using-audiocapturer-for-recording.md)或[audio.createAudioCapturer](../reference/apis/js-apis-audio.md#audiocreateaudiocapturer8),完成AudioCapturer的创建,然后可以通过以下两种方式查看音频流状态的变化:
8
9- 方法1:直接查看AudioCapturer的[state](../reference/apis/js-apis-audio.md#属性):
10
11  ```ts
12  let audioCapturerState = audioCapturer.state;
13  console.info(`Current state is: ${audioCapturerState }`)
14  ```
15
16- 方法2:注册stateChange监听AudioCapturer的状态变化:
17
18  ```ts
19  audioCapturer.on('stateChange', (capturerState) => {
20    console.info(`State change to: ${capturerState}`)
21  });
22  ```
23
24获取state后可对照[AudioState](../reference/apis/js-apis-audio.md#audiostate8)来进行相应的操作,比如显示录制结束的提示等。
25
26## 读取或监听所有录制流的变化
27
28如果部分应用需要查询获取所有音频流的变化信息,可以通过AudioStreamManager读取或监听所有音频流的变化。
29
30> **说明:**
31>
32> 对于标记为系统接口(system api)的音频流变化信息需要系统级别应用才可查看,若应用不是系统应用,将无法获取准确信息。
33
34如下为音频流管理调用关系图:
35
36![Invoking relationship of recording stream management](figures/invoking-relationship-recording-stream-mgmt.png)
37
38在进行应用开发的过程中,开发者需要使用getStreamManager()创建一个AudioStreamManager实例,进而通过该实例管理音频流。开发者可通过调用on('audioCapturerChange')监听音频流的变化,在音频流状态变化、设备变化时获得通知,同时可通过off('audioCapturerChange')取消相关事件的监听。另外,开发者可以通过主动调用getCurrentAudioCapturerInfoArray()查询录制流的唯一ID、录制流客户端的UID、以及流状态等信息。
39
40详细API含义可参考[音频管理API文档AudioStreamManager](../reference/apis/js-apis-audio.md#audiostreammanager9)。
41
42
43## 开发步骤及注意事项
44
451. 创建AudioStreamManager实例。
46     在使用AudioStreamManager的API前,需要使用getStreamManager()创建一个AudioStreamManager实例。
47
48   ```ts
49   import audio from '@ohos.multimedia.audio';
50   let audioManager = audio.getAudioManager();
51   let audioStreamManager = audioManager.getStreamManager();
52   ```
53
542. 使用on('audioCapturerChange')监听音频录制流更改事件。 如果音频流监听应用需要在音频录制流状态变化、设备变化时获取通知,可以订阅该事件。
55
56   ```ts
57   audioStreamManager.on('audioCapturerChange', (AudioCapturerChangeInfoArray) =>  {
58     for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
59       console.info(`## CapChange on is called for element ${i} ##`);
60       console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
61       console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
62       console.info(`Flag  ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
63       let devDescriptor = AudioCapturerChangeInfoArray[i].deviceDescriptors;
64       for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
65         console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
66         console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
67         console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
68         console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
69         console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
70         console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
71         console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
72         console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
73       }
74     }
75   });
76   ```
77
783. (可选)使用off('audioCapturerChange')取消监听音频录制流变化。
79
80   ```ts
81   audioStreamManager.off('audioCapturerChange');
82   console.info('CapturerChange Off is called');
83   ```
84
854. (可选)使用getCurrentAudioCapturerInfoArray()获取当前音频录制流的信息。
86     该接口可获取音频录制流唯一ID,音频录制客户端的UID,音频状态以及音频捕获器的其他信息。
87   > **说明:**
88   > 对所有音频流状态进行监听的应用需要[申请权限](../security/accesstoken-guidelines.md)ohos.permission.USE_BLUETOOTH,否则无法获得实际的设备名称和设备地址信息,查询到的设备名称和设备地址(蓝牙设备的相关属性)将为空字符串。
89
90   ```ts
91   async function getCurrentAudioCapturerInfoArray(){
92     await audioStreamManager.getCurrentAudioCapturerInfoArray().then( function (AudioCapturerChangeInfoArray) {
93       console.info('getCurrentAudioCapturerInfoArray  Get Promise Called ');
94       if (AudioCapturerChangeInfoArray != null) {
95         for (let i = 0; i < AudioCapturerChangeInfoArray.length; i++) {
96           console.info(`StreamId for ${i} is: ${AudioCapturerChangeInfoArray[i].streamId}`);
97           console.info(`Source for ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.source}`);
98           console.info(`Flag  ${i} is: ${AudioCapturerChangeInfoArray[i].capturerInfo.capturerFlags}`);
99           for (let j = 0; j < AudioCapturerChangeInfoArray[i].deviceDescriptors.length; j++) {
100             console.info(`Id: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].id}`);
101             console.info(`Type: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceType}`);
102             console.info(`Role: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].deviceRole}`);
103             console.info(`Name: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].name}`);
104             console.info(`Address: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].address}`);
105             console.info(`SampleRates: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].sampleRates[0]}`);
106             console.info(`ChannelCounts ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelCounts[0]}`);
107             console.info(`ChannelMask: ${i} : ${AudioCapturerChangeInfoArray[i].deviceDescriptors[j].channelMasks}`);
108           }
109         }
110       }
111     }).catch((err) => {
112       console.error(`Invoke getCurrentAudioCapturerInfoArray failed, code is ${err.code}, message is ${err.message}`);
113     });
114   }
115   ```
116