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> | No device is available. 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-audio-kit/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
34import { audio } from '@kit.AudioKit';
35import { BusinessError } from '@kit.BasicServicesKit';
36
37let audioManager = audio.getAudioManager();
38let audioRoutingManager = audioManager.getRoutingManager();
39let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
40    deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
41    deviceType: audio.DeviceType.SPEAKER,
42    id: 1,
43    name: "",
44    address: "",
45    sampleRates: [44100],
46    channelCounts: [2],
47    channelMasks: [0],
48    networkId: audio.LOCAL_NETWORK_ID,
49    interruptGroupId: 1,
50    volumeGroupId: 1,
51    displayName: ""
52}];
53
54async function selectOutputDevice(): Promise<void> {
55    audioRoutingManager.selectOutputDevice(outputAudioDeviceDescriptor, (err: BusinessError) => {
56        if (err) {
57            console.error(`Invoke selectOutputDevice failed, code is ${err.code}, message is ${err.message}`);
58        } else {
59            console.info('Invoke selectOutputDevice succeeded.');
60        }
61    });
62}
63```
64
65### Continuing the Playing of the Specified Audio Stream
66
671. [Obtain the output device information](audio-output-device-management.md#obtaining-output-device-information).
68
692. Create an **AudioRendererFilter** instance, with **uid** to specify an application and **rendererId** to specify an audio stream.
70
713. Create an **AudioDeviceDescriptor** instance to describe an audio output device.
72
734. Call **selectOutputDeviceByFilter** to select a remote device, on which the specified audio stream will continue playing.
74 
75```ts
76import { audio } from '@kit.AudioKit';
77import { BusinessError } from '@kit.BasicServicesKit';
78
79let audioManager = audio.getAudioManager();
80let audioRoutingManager = audioManager.getRoutingManager();
81let outputAudioRendererFilter: audio.AudioRendererFilter  = {
82    uid: 20010041,
83    rendererInfo: {
84        usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
85        rendererFlags: 0 } as audio.AudioRendererInfo,
86    rendererId: 0 };
87
88let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
89    deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
90    deviceType: audio.DeviceType.SPEAKER,
91    id: 1,
92    name: "",
93    address: "",
94    sampleRates: [44100],
95    channelCounts: [2],
96    channelMasks: [0],
97    networkId: audio.LOCAL_NETWORK_ID,
98    interruptGroupId: 1,
99    volumeGroupId: 1,
100    displayName: ""
101}];
102async function selectOutputDeviceByFilter(): Promise<void> {
103    audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor, (err: BusinessError) => {
104        if (err) {
105            console.error(`Invoke selectOutputDeviceByFilter failed, code is ${err.code}, message is ${err.message}`);
106        } else {
107            console.info('Invoke selectOutputDeviceByFilter succeeded.');
108        }
109    });
110}
111```
112