1# Global Audio Output Device Management 2 3You can use APIs to manage audio output devices, including querying audio output device information and listening for device connection status changes. For details about the APIs, see [AudioRoutingManager](../../reference/apis-audio-kit/arkts-apis-audio-AudioRoutingManager.md). 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> **NOTE** 48> 49> The listener captures all changes in device connections. It is not recommended that the changes be used as a basis for handling automatic pausing in applications. If an application needs to manage services related to automatic pause, it should consider the [reasons behind changes in the audio stream output device](audio-output-device-change.md). 50 51```ts 52import { audio } from '@kit.AudioKit'; 53 54// Listen for connection state changes of audio devices. 55audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (deviceChanged: audio.DeviceChangeAction) => { 56 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. 57 console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length}`); 58 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole}`); // Device role. 59 console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType}`); // Device type. 60}); 61 62// Cancel the listener for the connection state changes of audio devices. 63audioRoutingManager.off('deviceChange'); 64``` 65 66<!--Del--> 67## Selecting an Audio Output Device (for System Applications only) 68 69Currently, 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/arkts-apis-audio-t.md#audiodevicedescriptors). 70 71> **NOTE** 72> 73> 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). 74 75```ts 76import { audio } from '@kit.AudioKit'; 77import { BusinessError } from '@kit.BasicServicesKit'; 78 79let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{ 80 deviceRole : audio.DeviceRole.OUTPUT_DEVICE, 81 deviceType : audio.DeviceType.SPEAKER, 82 id : 1, 83 name : "", 84 address : "", 85 sampleRates : [44100], 86 channelCounts : [2], 87 channelMasks : [0], 88 networkId : audio.LOCAL_NETWORK_ID, 89 interruptGroupId : 1, 90 volumeGroupId : 1, 91 displayName : "" 92}]; 93 94async function selectOutputDevice() { 95 audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor).then(() => { 96 console.info('Invoke selectOutputDevice succeeded.'); 97 }).catch((err: BusinessError) => { 98 console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`); 99 }); 100} 101``` 102<!--DelEnd--> 103 104## Obtaining Information About the Output Device with the Highest Priority 105 106Call **getPreferOutputDeviceForRendererInfo()** to obtain the output device with the highest priority. 107 108> **NOTE** 109> 110> The output device with the highest priority is the device that will output audio. 111 112```ts 113import { audio } from '@kit.AudioKit'; 114import { BusinessError } from '@kit.BasicServicesKit'; 115 116let rendererInfo: audio.AudioRendererInfo = { 117 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario. 118 rendererFlags: 0 // AudioRenderer flag. 119}; 120 121async function getPreferOutputDeviceForRendererInfo() { 122 audioRoutingManager.getPreferOutputDeviceForRendererInfo(rendererInfo).then((desc: audio.AudioDeviceDescriptors) => { 123 console.info(`device descriptor: ${desc}`); 124 }).catch((err: BusinessError) => { 125 console.error(`Result ERROR: ${err}`); 126 }) 127} 128``` 129 130## Listening for Changes of the Output Device with the Highest Priority 131 132```ts 133import { audio } from '@kit.AudioKit'; 134 135let rendererInfo: audio.AudioRendererInfo = { 136 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario. 137 rendererFlags: 0 // AudioRenderer flag. 138}; 139 140// Listen for changes of the output device with the highest priority. 141audioRoutingManager.on('preferOutputDeviceChangeForRendererInfo', rendererInfo, (desc: audio.AudioDeviceDescriptors) => { 142 console.info(`device change descriptor : ${desc[0].deviceRole}`); // Device role. 143 console.info(`device change descriptor : ${desc[0].deviceType}`); // Device type. 144}); 145 146// Cancel the listening for changes of the output device with the highest priority. 147audioRoutingManager.off('preferOutputDeviceChangeForRendererInfo'); 148``` 149