• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Distributed Audio Playback (for System Applications Only)
2
3Distributed audio playback enables an application to continue audio playback on another device in the same network.
4
5You can use distributed audio playback to transfer all audio streams or the specified audio stream being played on the current device to a remote device.
6
7## How to Develop
8
9Before continuing audio playback on another device in the same network, you must obtain the device list on the network and listen for device connection state changes. For details, see [Audio Output Device Management](audio-output-device-management.md).
10
11When obtaining the device list on the network, you can specify **DeviceFlag** to filter out the required devices.
12
13| Name| Description|
14| -------- | -------- |
15| NONE_DEVICES_FLAG<sup>9+</sup> | None. This is a system API.|
16| OUTPUT_DEVICES_FLAG | Local output device.|
17| INPUT_DEVICES_FLAG | Local input device.|
18| ALL_DEVICES_FLAG | Local input and output device.|
19| DISTRIBUTED_OUTPUT_DEVICES_FLAG<sup>9+</sup> | Remote output device. This is a system API.|
20| DISTRIBUTED_INPUT_DEVICES_FLAG<sup>9+</sup> | Remote input device. This is a system API.|
21| ALL_DISTRIBUTED_DEVICES_FLAG<sup>9+</sup> | Remote input and output device. This is a system API.|
22
23For details about the API reference, see [AudioRoutingManager](../reference/apis/js-apis-audio.md#audioroutingmanager9).
24
25### Continuing the Playing of All Audio Streams
26
271. [Obtain the output device information](audio-output-device-management.md#obtaining-output-device-information).
28
292. Create an **AudioDeviceDescriptor** instance to describe an audio output device.
30
313. Call **selectOutputDevice** to select a remote device, on which all the audio streams will continue playing.
32
33```ts
34let outputAudioDeviceDescriptor = [{
35  deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
36  deviceType: audio.DeviceType.SPEAKER,
37  id: 1,
38  name: "",
39  address: "",
40  sampleRates: [44100],
41  channelCounts: [2],
42  channelMasks: [0],
43  networkId: audio.LOCAL_NETWORK_ID,
44  interruptGroupId: 1,
45  volumeGroupId: 1,
46}];
47
48async function selectOutputDevice() {
49  audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor, (err) => {
50    if (err) {
51      console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`);
52    } else {
53      console.info('Invoke selectOutputDevice succeeded.');
54    }
55  });
56}
57```
58
59### Continuing the Playing of the Specified Audio Stream
60
611. [Obtain the output device information](audio-output-device-management.md#obtaining-output-device-information).
62
632. Create an **AudioRendererFilter** instance, with **uid** to specify an application and **rendererId** to specify an audio stream.
64
653. Create an **AudioDeviceDescriptor** instance to describe an audio output device.
66
674. Call **selectOutputDeviceByFilter** to select a remote device, on which the specified audio stream will continue playing.
68
69```ts
70let outputAudioRendererFilter = {
71  uid: 20010041,
72  rendererInfo: {
73    content: audio.ContentType.CONTENT_TYPE_MUSIC,
74    usage: audio.StreamUsage.STREAM_USAGE_MEDIA,
75    rendererFlags: 0 },
76  rendererId: 0 };
77
78let outputAudioDeviceDescriptor = [{
79  deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
80  deviceType: audio.DeviceType.SPEAKER,
81  id: 1,
82  name: "",
83  address: "",
84  sampleRates: [44100],
85  channelCounts: [2],
86  channelMasks: [0],
87  networkId: audio.LOCAL_NETWORK_ID,
88  interruptGroupId: 1,
89  volumeGroupId: 1,
90}];
91
92async function selectOutputDeviceByFilter() {
93  audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor, (err) => {
94    if (err) {
95      console.error(`Invoke selectOutputDeviceByFilter failed, code is ${err.code}, message is ${err.message}`);
96    } else {
97      console.info('Invoke selectOutputDeviceByFilter succeeded.');
98    }
99  });
100}
101```
102