1# Managing Global Audio Output Devices
2
3If a device is connected to multiple audio output devices, you can use **AudioRoutingManager** to specify an audio output device to play audio. For details about the API reference, see [AudioRoutingManager](../../reference/apis-audio-kit/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 '@kit.AudioKit';  // 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 Output Device Types
18
19The table below lists the supported audio output devices.
20
21| Name| Value| Description|
22| -------- | -------- | -------- |
23| EARPIECE | 1 | Earpiece.|
24| SPEAKER | 2 | Speaker.|
25| WIRED_HEADSET | 3 | Wired headset with a microphone.|
26| WIRED_HEADPHONES | 4 | Wired headset without microphone.|
27| BLUETOOTH_SCO | 7 | Bluetooth device using Synchronous Connection Oriented (SCO) links.|
28| BLUETOOTH_A2DP | 8 | Bluetooth device using Advanced Audio Distribution Profile (A2DP) links.|
29| USB_HEADSET | 22 | USB Type-C headset.|
30
31## Obtaining Output Device Information
32
33Use **getDevices()** to obtain information about all the output devices.
34
35```ts
36import { audio } from '@kit.AudioKit';
37
38audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data: audio.AudioDeviceDescriptors) => {
39  console.info('Promise returned to indicate that the device list is obtained.');
40});
41```
42
43## Listening for Device Connection State Changes
44
45Set a listener to listen for changes of the device connection state. When a device is connected or disconnected, a callback is triggered.
46
47```ts
48import { audio } from '@kit.AudioKit';
49
50// Listen for connection state changes of audio devices.
51audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (deviceChanged: audio.DeviceChangeAction) => {
52  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.
53  console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length}`);
54  console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole}`);  // Device role.
55  console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType}`);  // Device type.
56});
57
58// Cancel the listener for the connection state changes of audio devices.
59audioRoutingManager.off('deviceChange');
60```
61
62<!--Del-->
63## Selecting an Audio Output Device (for System Applications only)
64
65Currently, only one output device can be selected, and the device ID is used as the unique identifier. For details about audio device descriptors, see [AudioDeviceDescriptors](../../reference/apis-audio-kit/js-apis-audio.md#audiodevicedescriptors).
66
67> **NOTE**
68>
69> 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).
70
71```ts
72import { audio } from '@kit.AudioKit';
73import { BusinessError } from '@kit.BasicServicesKit';
74
75let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
76    deviceRole : audio.DeviceRole.OUTPUT_DEVICE,
77    deviceType : audio.DeviceType.SPEAKER,
78    id : 1,
79    name : "",
80    address : "",
81    sampleRates : [44100],
82    channelCounts : [2],
83    channelMasks : [0],
84    networkId : audio.LOCAL_NETWORK_ID,
85    interruptGroupId : 1,
86    volumeGroupId : 1,
87    displayName : ""
88}];
89
90async function selectOutputDevice() {
91  audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor).then(() => {
92    console.info('Invoke selectOutputDevice succeeded.');
93  }).catch((err: BusinessError) => {
94    console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`);
95  });
96}
97```
98<!--DelEnd-->
99
100## Obtaining Information About the Output Device with the Highest Priority
101
102Call **getPreferOutputDeviceForRendererInfo()** to obtain the output device with the highest priority.
103
104> **NOTE**
105>
106> The output device with the highest priority is the device that will output audio.
107
108```ts
109import { audio } from '@kit.AudioKit';
110import { BusinessError } from '@kit.BasicServicesKit';
111
112let rendererInfo: audio.AudioRendererInfo = {
113    usage : audio.StreamUsage.STREAM_USAGE_MUSIC,
114    rendererFlags : 0
115};
116
117async function getPreferOutputDeviceForRendererInfo() {
118  audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => {
119    console.info(`device descriptor: ${desc}`);
120  }).catch((err: BusinessError) => {
121    console.error(`Result ERROR: ${err}`);
122  })
123}
124```
125
126## Listening for Changes of the Output Device with the Highest Priority
127
128```ts
129import { audio } from '@kit.AudioKit';
130
131let rendererInfo: audio.AudioRendererInfo = {
132    usage : audio.StreamUsage.STREAM_USAGE_MUSIC,
133    rendererFlags : 0
134};
135
136// Listen for changes of the output device with the highest priority.
137audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => {
138    console.info(`device change descriptor : ${desc[0].deviceRole}`);  // Device role.
139    console.info(`device change descriptor : ${desc[0].deviceType}`);  // Device type.
140});
141
142// Cancel the listening for changes of the output device with the highest priority.
143audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo');
144```
145