1# 路由、设备管理开发指导 2 3## 简介 4 5AudioRoutingManager提供了音频路由、设备管理的方法。开发者可以通过本指导了解应用如何通过AudioRoutingManager获取当前工作的输入、输出音频设备,监听音频设备的连接状态变化,激活通信设备等。 6 7## 运作机制 8 9该模块提供了路由、设备管理模块常用接口 10 11**图1** 路由、设备管理常用接口 12 13 14 15**说明:** AudioRoutingManager主要接口有:获取设备列表信息、监听与取消监听设备连接状态、激活通信设备、查询通信设备激活状态。更多介绍请参考[API参考文档](../reference/apis/js-apis-audio.md)。 16 17 18## 开发指导 19 20详细API含义可参考:[音频路由、设备管理API文档AudioRoutingManager](../reference/apis/js-apis-audio.md#audioroutingmanager9) 21 221. 创建AudioRoutingManager实例。 23 24 在使用AudioRoutingManager的API前,需要使用getRoutingManager创建一个AudioRoutingManager实例。 25 26 ```js 27 import audio from '@ohos.multimedia.audio'; 28 async loadAudioRoutingManager() { 29 var audioRoutingManager = await audio.getAudioManager().getRoutingManager(); 30 console.info('audioRoutingManager------create-------success.'); 31 } 32 33 ``` 34 352. (可选)获取设备列表信息、监听设备链接状态变化。 36 37 如果开发者需要获取设备列表信息(输入、输出、分布式输入、分布式输出等),或者监听音频设备的链接状态变化时,可参考并调用以下接口。 38 39 ```js 40 import audio from '@ohos.multimedia.audio'; 41 //创建AudioRoutingManager实例 42 async loadAudioRoutingManager() { 43 var audioRoutingManager = await audio.getAudioManager().getRoutingManager(); 44 console.info('audioRoutingManager------create-------success.'); 45 } 46 //获取全部音频设备信息(开发者可以根据自身需要填入适当的DeviceFlag) 47 async getDevices() { 48 await loadAudioRoutingManager(); 49 await audioRoutingManager.getDevices(audio.DeviceFlag.ALL_DEVICES_FLAG).then((data) => { 50 console.info(`getDevices success and data is: ${JSON.stringify(data)}.`); 51 }); 52 } 53 //监听音频设备状态变化 54 async onDeviceChange() { 55 await loadAudioRoutingManager(); 56 await audioRoutingManager.on('deviceChange', audio.DeviceFlag.ALL_DEVICES_FLAG, (deviceChanged) => { 57 console.info('on device change type : ' + deviceChanged.type); 58 console.info('on device descriptor size : ' + deviceChanged.deviceDescriptors.length); 59 console.info('on device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 60 console.info('on device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 61 }); 62 } 63 //取消监听音频设备状态变化 64 async offDeviceChange() { 65 await loadAudioRoutingManager(); 66 await audioRoutingManager.off('deviceChange', (deviceChanged) => { 67 console.info('off device change type : ' + deviceChanged.type); 68 console.info('off device descriptor size : ' + deviceChanged.deviceDescriptors.length); 69 console.info('off device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole); 70 console.info('off device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType); 71 }); 72 } 73 //综合调用:先查询所有设备,设置监听,然后开发者手动变更设备连接(例如有线耳机),再次查询所有设备,最后取消设备状态变化的监听。 74 async test(){ 75 await getDevices(); 76 await onDeviceChange()(); 77 //开发者手动断开/连接设备 78 await getDevices(); 79 await offDeviceChange(); 80 } 81 ``` 82 833. (可选)设置通信设备激活并查询激活状态。 84 85 ```js 86 import audio from '@ohos.multimedia.audio'; 87 //创建AudioRoutingManager实例 88 async loadAudioRoutingManager() { 89 var audioRoutingManager = await audio.getAudioManager().getRoutingManager(); 90 console.info('audioRoutingManager------create-------success.'); 91 } 92 //设置通信设备激活状态 93 async setCommunicationDevice() { 94 await loadAudioRoutingManager(); 95 await audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true).then(() => { 96 console.info('setCommunicationDevice true is success.'); 97 }); 98 } 99 //查询通信设备激活状态 100 async isCommunicationDeviceActive() { 101 await loadAudioRoutingManager(); 102 await audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER).then((value) => { 103 console.info(`CommunicationDevice state is: ${value}.`); 104 }); 105 } 106 //综合调用:先设置设备激活,然后查询设备状态。 107 async test(){ 108 await setCommunicationDevice(); 109 await isCommunicationDeviceActive(); 110 } 111 ``` 112 113