• 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
34import audio from '@ohos.multimedia.audio';
35import { BusinessError } from '@ohos.base';
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 '@ohos.multimedia.audio';
77import { BusinessError } from '@ohos.base';
78
79let audioManager = audio.getAudioManager();
80let audioRoutingManager = audioManager.getRoutingManager();
81let outputAudioRendererFilter: audio.AudioRendererFilter  = {
82    uid: 20010041,
83    rendererInfo: {
84        content: audio.ContentType.CONTENT_TYPE_MUSIC,
85        usage: audio.StreamUsage.STREAM_USAGE_MEDIA,
86        rendererFlags: 0 } as audio.AudioRendererInfo,
87    rendererId: 0 };
88
89let outputAudioDeviceDescriptor: audio.AudioDeviceDescriptors = [{
90    deviceRole: audio.DeviceRole.OUTPUT_DEVICE,
91    deviceType: audio.DeviceType.SPEAKER,
92    id: 1,
93    name: "",
94    address: "",
95    sampleRates: [44100],
96    channelCounts: [2],
97    channelMasks: [0],
98    networkId: audio.LOCAL_NETWORK_ID,
99    interruptGroupId: 1,
100    volumeGroupId: 1,
101    displayName: ""
102}];
103async function selectOutputDeviceByFilter(): Promise<void> {
104    audioRoutingManager.selectOutputDeviceByFilter(outputAudioRendererFilter, outputAudioDeviceDescriptor, (err: BusinessError) => {
105        if (err) {
106            console.error(`Invoke selectOutputDeviceByFilter failed, code is ${err.code}, message is ${err.message}`);
107        } else {
108            console.info('Invoke selectOutputDeviceByFilter succeeded.');
109        }
110    });
111}
112```
113