• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Output Device Management (ArkTS)
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 '@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 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 '@ohos.multimedia.audio';
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 '@ohos.multimedia.audio';
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## Selecting an Audio Output Device (for System Applications only)
63
64Currently, 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).
65
66> **NOTE**
67>
68> 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).
69
70```ts
71import audio from '@ohos.multimedia.audio';
72import { BusinessError } from '@ohos.base';
73
74let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
75    deviceRole : audio.DeviceRole.OUTPUT_DEVICE,
76    deviceType : audio.DeviceType.SPEAKER,
77    id : 1,
78    name : "",
79    address : "",
80    sampleRates : [44100],
81    channelCounts : [2],
82    channelMasks : [0],
83    networkId : audio.LOCAL_NETWORK_ID,
84    interruptGroupId : 1,
85    volumeGroupId : 1,
86    displayName : ""
87}];
88
89async function selectOutputDevice() {
90  audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor).then(() => {
91    console.info('Invoke selectOutputDevice succeeded.');
92  }).catch((err: BusinessError) => {
93    console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`);
94  });
95}
96```
97
98## Obtaining Information About the Output Device with the Highest Priority
99
100Call **getPreferOutputDeviceForRendererInfo()** to obtain the output device with the highest priority.
101
102> **NOTE**
103>
104> The output device with the highest priority is the device that will output audio.
105
106```ts
107import audio from '@ohos.multimedia.audio';
108import { BusinessError } from '@ohos.base';
109
110let rendererInfo: audio.AudioRendererInfo = {
111    usage : audio.StreamUsage.STREAM_USAGE_MUSIC,
112    rendererFlags : 0
113}
114
115async function getPreferOutputDeviceForRendererInfo() {
116  audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => {
117    console.info(`device descriptor: ${desc}`);
118  }).catch((err: BusinessError) => {
119    console.error(`Result ERROR: ${err}`);
120  })
121}
122```
123
124## Listening for Changes of the Output Device with the Highest Priority
125
126```ts
127import audio from '@ohos.multimedia.audio';
128
129let rendererInfo: audio.AudioRendererInfo = {
130    usage : audio.StreamUsage.STREAM_USAGE_MUSIC,
131    rendererFlags : 0
132}
133
134// Listen for changes of the output device with the highest priority.
135audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => {
136    console.info(`device change descriptor : ${desc[0].deviceRole}`);  // Device role.
137    console.info(`device change descriptor : ${desc[0].deviceType}`);  // Device type.
138});
139
140// Cancel the listening for changes of the output device with the highest priority.
141audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo');
142```
143