• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Input Device Management
2
3If a device is connected to multiple audio input devices, you can use **AudioRoutingManager** to specify an audio input device to record audio. For details about the API reference, see [AudioRoutingManager](../reference/apis/js-apis-audio.md#audioroutingmanager9).
4
5## Creating an AudioRoutingManager Instance
6
7Before using **AudioRoutingManager** to manage audio devices, import the audio module and create an **AudioManager** instance.
8
9```ts
10import audio from '@ohos.multimedia.audio'; // Import the audio module.
11
12let audioManager = audio.getAudioManager(); // Create an AudioManager instance.
13
14let audioRoutingManager = audioManager.getRoutingManager(); // Call an API of AudioManager to create an AudioRoutingManager instance.
15```
16
17## Supported Audio Input Device Types
18
19The table below lists the supported audio input devices.
20
21| Name| Value| Description|
22| -------- | -------- | -------- |
23| WIRED_HEADSET | 3 | Wired headset with a microphone.|
24| BLUETOOTH_SCO | 7 | Bluetooth device using Synchronous Connection Oriented (SCO) links.|
25| MIC | 15 | Microphone.|
26| USB_HEADSET | 22 | USB Type-C headset.|
27
28## Obtaining Input Device Information
29
30Use **getDevices()** to obtain information about all the input devices.
31
32```ts
33audioRoutingManager.getDevices(audio.DeviceFlag.INPUT_DEVICES_FLAG).then((data) => {
34  console.info('Promise returned to indicate that the device list is obtained.');
35});
36```
37
38## Listening for Device Connection State Changes
39
40Set a listener to listen for changes of the device connection state. When a device is connected or disconnected, a callback is triggered.
41
42```ts
43// Listen for connection state changes of audio devices.
44audioRoutingManager.on('deviceChange', audio.DeviceFlag.INPUT_DEVICES_FLAG, (deviceChanged) => {
45  console.info('device change type: ' + deviceChanged.type); // Device connection state change. The value 0 means that the device is connected and 1 means that the device is disconnected.
46  console.info('device descriptor size : ' + deviceChanged.deviceDescriptors.length);
47  console.info('device change descriptor: ' + deviceChanged.deviceDescriptors[0].deviceRole); // Device role.
48  console.info('device change descriptor: ' + deviceChanged.deviceDescriptors[0].deviceType); // Device type.
49});
50
51// Cancel the listener for the connection state changes of audio devices.
52audioRoutingManager.off('deviceChange', (deviceChanged) => {
53  console.info('Should be no callback.');
54});
55```
56
57## Selecting an Audio Input Device (for System Applications only)
58
59Currently, only one input device can be selected, and the device ID is used as the unique identifier. For details about audio device descriptors, see [AudioDeviceDescriptors](../reference/apis/js-apis-audio.md#audiodevicedescriptors).
60
61> **NOTE**
62>
63> The user can connect to a group of audio devices (for example, a pair of Bluetooth headsets), but the system treats them as one device (a group of devices that share the same device ID).
64
65```ts
66let inputAudioDeviceDescriptor = [{
67    deviceRole : audio.DeviceRole.INPUT_DEVICE,
68    deviceType : audio.DeviceType.EARPIECE,
69    id : 1,
70    name : "",
71    address : "",
72    sampleRates : [44100],
73    channelCounts : [2],
74    channelMasks : [0],
75    networkId : audio.LOCAL_NETWORK_ID,
76    interruptGroupId : 1,
77    volumeGroupId : 1,
78}];
79
80async function getRoutingManager(){
81    audioRoutingManager.selectInputDevice(inputAudioDeviceDescriptor).then(() => {
82      console.info('Invoke selectInputDevice succeeded.');
83    }).catch((err) => {
84      console.error(`Invoke selectInputDevice failed, code is ${err.code}, message is ${err.message}`);
85    });
86}
87
88```
89