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